多线程多进程编程指南

多线程多进程编程指南

目录


Python 多线程编程

1. 基础线程创建和管理

import threading
import time

def worker(name, delay):
    """工作线程函数"""
    for i in range(5):
        print(f"线程 {name} 正在工作: {i}")
        time.sleep(delay)
    print(f"线程 {name} 完成工作")

def basic_threading_example():
    """基础多线程示例"""
    # 创建线程
    thread1 = threading.Thread(target=worker, args=("A", 1))
    thread2 = threading.Thread(target=worker, args=("B", 1.5))
    
    # 启动线程
    thread1.start()
    thread2.start()
    
    # 等待线程完成
    thread1.join()
    thread2.join()
    
    print("所有线程完成")

if __name__ == "__main__":
    basic_threading_example()

2. 线程同步 - 锁机制

import threading
import time

# 全局变量和锁
counter = 0
lock = threading.Lock()

def increment_counter(name, times):
    """使用锁保护共享资源"""
    global counter
    for i in range(times):
        with lock:  # 使用with语句自动管理锁
            temp = counter
            time.sleep(0.001)  # 模拟处理时间
            counter = temp + 1
            print(f"线程 {name}: counter = {counter}")

def lock_example():
    """锁机制示例"""
    global counter
    counter = 0
    
    # 创建多个线程
    threads = []
    for i in range(3):
        t = threading.Thread(target=increment_counter, args=(f"Thread-{i}", 5))
        threads.append(t)
        t.start()
    
    # 等待所有线程完成
    for t in threads:
        t.join()
    
    print(f"最终计数器值: {counter}")

if __name__ == "__main__":
    lock_example()

3. 线程间通信 - Queue

import threading
import queue
import time
import random

def producer(q, name):
    """生产者线程"""
    for i in range(5):
        item = f"{name}-item-{i}"
        q.put(item)
        print(f"生产者 {name} 生产了: {item}")
        time.sleep(random.uniform(0.5, 1.5))
    
    # 发送结束信号
    q.put(None)

def consumer(q, name):
    """消费者线程"""
    while True:
        item = q.get()
        if item is None:
            q.put(None)  # 传递结束信号给其他消费者
            break
        
        print(f"消费者 {name} 消费了: {item}")
        time.sleep(random.uniform(0.5, 1.0))
        q.task_done()

def queue_example():
    """队列通信示例"""
    # 创建队列
    q = queue.Queue(maxsize=10)
    
    # 创建生产者线程
    producer_thread = threading.Thread(target=producer, args=(q, "Producer-1"))
    
    # 创建消费者线程
    consumer_threads = []
    for i in range(2):
        t = threading.Thread(target=consumer, args=(q, f"Consumer-{i}"))
        consumer_threads.append(t)
    
    # 启动所有线程
    producer_thread.start()
    for t in consumer_threads:
        t.start()
    
    # 等待生产者完成
    producer_thread.join()
    
    # 等待消费者完成
    for t in consumer_threads:
        t.join()
    
    print("队列通信示例完成")

if __name__ == "__main__":
    queue_example()

4. 线程池 - ThreadPoolExecutor

import concurrent.futures
import time
import requests

def download_url(url):
    """模拟下载任务"""
    try:
        response = requests.get(url, timeout=5)
        return f"URL: {url}, Status: {response.status_code}, Size: {len(response.content)}"
    except Exception as e:
        return f"URL: {url}, Error: {str(e)}"

def cpu_intensive_task(n):
    """CPU密集型任务"""
    result = 0
    for i in range(n):
        result += i * i
    return result

def thread_pool_example():
    """线程池示例"""
    urls = [
        "https://httpbin.org/delay/1",
        "https://httpbin.org/delay/2",
        "https://httpbin.org/delay/1",
        "https://httpbin.org/status/200",
        "https://httpbin.org/json"
    ]
    
    # I/O密集型任务 - 使用线程池
    print("=== I/O密集型任务 - 线程池 ===")
    start_time = time.time()
    
    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
        # 提交任务
        future_to_url = {executor.submit(download_url, url): url for url in urls}
        
        # 获取结果
        for future in concurrent.futures.as_completed(future_to_url):
            result = future.result()
            print(result)
    
    print(f"线程池完成时间: {time.time() - start_time:.2f}秒")
    
    # CPU密集型任务对比
    print("\n=== CPU密集型任务对比 ===")
    numbers = [100000, 200000, 150000, 300000]
    
    # 单线程
    start_time = time.time()
    results = [cpu_intensive_task(n) for n in numbers]
    single_thread_time = time.time() - start_time
    print(f"单线程时间: {single_thread_time:.2f}秒")
    
    # 多线程(由于GIL,可能不会更快)
    start_time = time.time()
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(cpu_intensive_task, numbers))
    multi_thread_time = time.time() - start_time
    print(f"多线程时间: {multi_thread_time:.2f}秒")

if __name__ == "__main__":
    thread_pool_example()

Python 多进程编程

1. 基础进程创建和管理

import multiprocessing
import time
import os

def worker_process(name, delay):
    """工作进程函数"""
    print(f"进程 {name} (PID: {os.getpid()}) 开始工作")
    for i in range(5):
        print(f"进程 {name} 正在工作: {i}")
        time.sleep(delay)
    print(f"进程 {name} 完成工作")

def basic_multiprocessing_example():
    """基础多进程示例"""
    print(f"主进程 PID: {os.getpid()}")
    
    # 创建进程
    process1 = multiprocessing.Process(target=worker_process, args=("A", 1))
    process2 = multiprocessing.Process(target=worker_process, args=("B", 1.5))
    
    # 启动进程
    process1.start()
    process2.start()
    
    # 等待进程完成
    process1.join()
    process2.join()
    
    print("所有进程完成")

if __name__ == "__main__":
    basic_multiprocessing_example()

2. 进程间通信 - Queue和Pipe

import multiprocessing
import time
import random

def producer_process(q, name):
    """生产者进程"""
    for i in range(5):
        item = f"{name}-item-{i}"
        q.put(item)
        print(f"生产者进程 {name} 生产了: {item}")
        time.sleep(random.uniform(0.5, 1.0))
    
    q.put(None)  # 结束信号

def consumer_process(q, name):
    """消费者进程"""
    while True:
        item = q.get()
        if item is None:
            break
        
        print(f"消费者进程 {name} 消费了: {item}")
        time.sleep(random.uniform(0.5, 1.0))

def queue_communication_example():
    """队列通信示例"""
    # 创建进程间队列
    q = multiprocessing.Queue()
    
    # 创建进程
    producer = multiprocessing.Process(target=producer_process, args=(q, "Producer"))
    consumer = multiprocessing.Process(target=consumer_process, args=(q, "Consumer"))
    
    # 启动进程
    producer.start()
    consumer.start()
    
    # 等待完成
    producer.join()
    consumer.join()
    
    print("队列通信示例完成")

def pipe_communication_example():
    """管道通信示例"""
    def sender(conn):
        """发送端"""
        for i in range(5):
            msg = f"消息-{i}"
            conn.send(msg)
            print(f"发送: {msg}")
            time.sleep(1)
        conn.close()
    
    def receiver(conn):
        """接收端"""
        while True:
            try:
                msg = conn.recv()
                print(f"接收: {msg}")
            except EOFError:
                break
    
    # 创建管道
    parent_conn, child_conn = multiprocessing.Pipe()
    
    # 创建进程
    sender_process = multiprocessing.Process(target=sender, args=(child_conn,))
    receiver_process = multiprocessing.Process(target=receiver, args=(parent_conn,))
    
    # 启动进程
    sender_process.start()
    receiver_process.start()
    
    # 等待完成
    sender_process.join()
    receiver_process.join()
    
    print("管道通信示例完成")

if __name__ == "__main__":
    print("=== 队列通信 ===")
    queue_communication_example()
    
    print("\n=== 管道通信 ===")
    pipe_communication_example()

3. 共享内存

import multiprocessing
import time

def worker_with_shared_memory(shared_array, shared_value, lock, worker_id):
    """使用共享内存的工作进程"""
    for i in range(5):
        with lock:
            # 修改共享数组
            shared_array[worker_id] += 1
            # 修改共享值
            shared_value.value += 1
            print(f"工作进程 {worker_id}: 数组[{worker_id}] = {shared_array[worker_id]}, 共享值 = {shared_value.value}")
        time.sleep(0.5)

def shared_memory_example():
    """共享内存示例"""
    # 创建共享数组和共享值
    shared_array = multiprocessing.Array('i', [0, 0, 0])  # 'i' 表示整数类型
    shared_value = multiprocessing.Value('i', 0)
    lock = multiprocessing.Lock()
    
    # 创建进程
    processes = []
    for i in range(3):
        p = multiprocessing.Process(
            target=worker_with_shared_memory,
            args=(shared_array, shared_value, lock, i)
        )
        processes.append(p)
        p.start()
    
    # 等待所有进程完成
    for p in processes:
        p.join()
    
    print(f"最终共享数组: {list(shared_array[:])}")
    print(f"最终共享值: {shared_value.value}")

if __name__ == "__main__":
    shared_memory_example()

4. 进程池 - ProcessPoolExecutor

import concurrent.futures
import multiprocessing
import time
import math

def cpu_intensive_task(n):
    """CPU密集型任务"""
    result = 0
    for i in range(n):
        result += math.sqrt(i)
    return result

def io_simulation_task(delay):
    """模拟I/O任务"""
    time.sleep(delay)
    return f"任务完成,延迟: {delay}秒"

def process_pool_example():
    """进程池示例"""
    # CPU密集型任务 - 使用进程池
    print("=== CPU密集型任务 - 进程池 ===")
    numbers = [1000000, 1500000, 2000000, 1200000]
    
    # 单进程
    start_time = time.time()
    results = [cpu_intensive_task(n) for n in numbers]
    single_process_time = time.time() - start_time
    print(f"单进程时间: {single_process_time:.2f}秒")
    
    # 多进程
    start_time = time.time()
    with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(cpu_intensive_task, numbers))
    multi_process_time = time.time() - start_time
    print(f"多进程时间: {multi_process_time:.2f}秒")
    print(f"加速比: {single_process_time / multi_process_time:.2f}x")
    
    # 使用submit和as_completed
    print("\n=== 使用submit和as_completed ===")
    delays = [1, 2, 1.5, 0.5, 3]
    
    with concurrent.futures.ProcessPoolExecutor(max_workers=3) as executor:
        # 提交任务
        future_to_delay = {executor.submit(io_simulation_task, delay): delay for delay in delays}
        
        # 按完成顺序获取结果
        for future in concurrent.futures.as_completed(future_to_delay):
            delay = future_to_delay[future]
            try:
                result = future.result()
                print(f"延迟 {delay}: {result}")
            except Exception as exc:
                print(f"延迟 {delay} 产生异常: {exc}")

if __name__ == "__main__":
    process_pool_example()

C/C++ 多线程编程

1. 基础线程创建 (C++11 std::thread)

#include <iostream>
#include <thread>
#include <chrono>
#include <vector>

void worker_function(int id, int delay_ms) {
    for (int i = 0; i < 5; ++i) {
        std::cout << "线程 " << id << " 正在工作: " << i << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
    }
    std::cout << "线程 " << id << " 完成工作" << std::endl;
}

class WorkerClass {
public:
    void operator()(int id, int delay_ms) {
        for (int i = 0; i < 3; ++i) {
            std::cout << "类线程 " << id << " 正在工作: " << i << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
        }
    }
};

int main() {
    std::cout << "=== 基础线程创建示例 ===" << std::endl;
    
    // 使用函数创建线程
    std::thread t1(worker_function, 1, 1000);
    std::thread t2(worker_function, 2, 1500);
    
    // 使用lambda表达式创建线程
    std::thread t3([](int id) {
        for (int i = 0; i < 3; ++i) {
            std::cout << "Lambda线程 " << id << " 正在工作: " << i << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(800));
        }
    }, 3);
    
    // 使用函数对象创建线程
    WorkerClass worker;
    std::thread t4(worker, 4, 1200);
    
    // 等待所有线程完成
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    
    std::cout << "所有线程完成" << std::endl;
    
    return 0;
}

2. 线程同步 - 互斥锁和条件变量

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
#include <random>

// 全局变量和同步原语
std::mutex mtx;
std::condition_variable cv;
std::queue<int> data_queue;
bool finished = false;

// 互斥锁示例
class Counter {
private:
    int count;
    std::mutex mtx;
    
public:
    Counter() : count(0) {}
    
    void increment() {
        std::lock_guard<std::mutex> lock(mtx);
        ++count;
        std::cout << "计数器: " << count << " (线程ID: " 
                  << std::this_thread::get_id() << ")" << std::endl;
    }
    
    int get_count() {
        std::lock_guard<std::mutex> lock(mtx);
        return count;
    }
};

void increment_counter(Counter& counter, int times) {
    for (int i = 0; i < times; ++i) {
        counter.increment();
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

// 生产者-消费者模式
void producer() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(1, 100);
    
    for (int i = 0; i < 10; ++i) {
        int value = dis(gen);
        
        {
            std::lock_guard<std::mutex> lock(mtx);
            data_queue.push(value);
            std::cout << "生产者生产: " << value << std::endl;
        }
        
        cv.notify_one();
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
    
    {
        std::lock_guard<std::mutex> lock(mtx);
        finished = true;
    }
    cv.notify_all();
}

void consumer(int id) {
    while (true) {
        std::unique_lock<std::mutex> lock(mtx);
        
        // 等待数据或完成信号
        cv.wait(lock, [] { return !data_queue.empty() || finished; });
        
        if (data_queue.empty() && finished) {
            break;
        }
        
        if (!data_queue.empty()) {
            int value = data_queue.front();
            data_queue.pop();
            lock.unlock();
            
            std::cout << "消费者 " << id << " 消费: " << value << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(800));
        }
    }
    
    std::cout << "消费者 " << id << " 退出" << std::endl;
}

int main() {
    std::cout << "=== 互斥锁示例 ===" << std::endl;
    
    Counter counter;
    std::vector<std::thread> threads;
    
    // 创建多个线程增加计数器
    for (int i = 0; i < 3; ++i) {
        threads.emplace_back(increment_counter, std::ref(counter), 5);
    }
    
    // 等待所有线程完成
    for (auto& t : threads) {
        t.join();
    }
    
    std::cout << "最终计数: " << counter.get_count() << std::endl;
    
    std::cout << "\n=== 生产者-消费者示例 ===" << std::endl;
    
    // 重置全局状态
    while (!data_queue.empty()) data_queue.pop();
    finished = false;
    
    // 创建生产者和消费者线程
    std::thread producer_thread(producer);
    std::thread consumer1(consumer, 1);
    std::thread consumer2(consumer, 2);
    
    // 等待完成
    producer_thread.join();
    consumer1.join();
    consumer2.join();
    
    return 0;
}

3. 原子操作

#include <iostream>
#include <thread>
#include <atomic>
#include <vector>
#include <chrono>

class AtomicCounter {
private:
    std::atomic<int> count{0};
    
public:
    void increment() {
        count.fetch_add(1);
    }
    
    void decrement() {
        count.fetch_sub(1);
    }
    
    int get_count() const {
        return count.load();
    }
    
    // 比较并交换
    bool compare_and_swap(int expected, int desired) {
        return count.compare_exchange_weak(expected, desired);
    }
};

void atomic_increment_worker(AtomicCounter& counter, int times) {
    for (int i = 0; i < times; ++i) {
        counter.increment();
        std::this_thread::sleep_for(std::chrono::microseconds(10));
    }
}

void atomic_decrement_worker(AtomicCounter& counter, int times) {
    for (int i = 0; i < times; ++i) {
        counter.decrement();
        std::this_thread::sleep_for(std::chrono::microseconds(10));
    }
}

// 无锁队列示例(简化版)
template<typename T>
class LockFreeQueue {
private:
    struct Node {
        std::atomic<T*> data{nullptr};
        std::atomic<Node*> next{nullptr};
    };
    
    std::atomic<Node*> head{new Node};
    std::atomic<Node*> tail{head.load()};
    
public:
    void enqueue(T item) {
        Node* new_node = new Node;
        T* data = new T(std::move(item));
        new_node->data.store(data);
        
        Node* prev_tail = tail.exchange(new_node);
        prev_tail->next.store(new_node);
    }
    
    bool dequeue(T& result) {
        Node* head_node = head.load();
        Node* next = head_node->next.load();
        
        if (next == nullptr) {
            return false;  // 队列为空
        }
        
        T* data = next->data.load();
        if (data == nullptr) {
            return false;
        }
        
        result = *data;
        head.store(next);
        delete data;
        delete head_node;
        
        return true;
    }
};

int main() {
    std::cout << "=== 原子操作示例 ===" << std::endl;
    
    AtomicCounter counter;
    std::vector<std::thread> threads;
    
    auto start_time = std::chrono::high_resolution_clock::now();
    
    // 创建增加线程
    for (int i = 0; i < 5; ++i) {
        threads.emplace_back(atomic_increment_worker, std::ref(counter), 1000);
    }
    
    // 创建减少线程
    for (int i = 0; i < 3; ++i) {
        threads.emplace_back(atomic_decrement_worker, std::ref(counter), 500);
    }
    
    // 等待所有线程完成
    for (auto& t : threads) {
        t.join();
    }
    
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
    
    std::cout << "最终计数: " << counter.get_count() << std::endl;
    std::cout << "执行时间: " << duration.count() << "ms" << std::endl;
    
    // 比较并交换示例
    std::cout << "\n=== 比较并交换示例 ===" << std::endl;
    AtomicCounter cas_counter;
    cas_counter.increment();  // 设置为1
    
    int expected = 1;
    bool success = cas_counter.compare_and_swap(expected, 10);
    std::cout << "CAS操作 (1->10): " << (success ? "成功" : "失败") 
              << ", 当前值: " << cas_counter.get_count() << std::endl;
    
    expected = 5;
    success = cas_counter.compare_and_swap(expected, 20);
    std::cout << "CAS操作 (5->20): " << (success ? "成功" : "失败") 
              << ", 当前值: " << cas_counter.get_count() << std::endl;
    
    return 0;
}

C/C++ 多进程编程

1. 基础进程创建 (Unix/Linux)

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <cstring>
#include <signal.h>

void child_process_work(int id) {
    std::cout << "子进程 " << id << " (PID: " << getpid() 
              << ") 开始工作" << std::endl;
    
    for (int i = 0; i < 5; ++i) {
        std::cout << "子进程 " << id << " 正在工作: " << i << std::endl;
        sleep(1);
    }
    
    std::cout << "子进程 " << id << " 完成工作" << std::endl;
}

int main() {
    std::cout << "主进程 PID: " << getpid() << std::endl;
    
    const int num_processes = 3;
    pid_t pids[num_processes];
    
    // 创建子进程
    for (int i = 0; i < num_processes; ++i) {
        pids[i] = fork();
        
        if (pids[i] == 0) {
            // 子进程代码
            child_process_work(i);
            exit(0);  // 子进程退出
        } else if (pids[i] < 0) {
            // fork失败
            std::cerr << "Fork失败" << std::endl;
            return 1;
        }
        // 父进程继续循环创建下一个子进程
    }
    
    // 父进程等待所有子进程完成
    for (int i = 0; i < num_processes; ++i) {
        int status;
        waitpid(pids[i], &status, 0);
        std::cout << "子进程 " << pids[i] << " 已完成" << std::endl;
    }
    
    std::cout << "所有子进程完成" << std::endl;
    return 0;
}

2. 进程间通信 - 管道

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstring>

int main() {
    int pipe_fd[2];  // 管道文件描述符
    pid_t pid;
    
    // 创建管道
    if (pipe(pipe_fd) == -1) {
        std::cerr << "管道创建失败" << std::endl;
        return 1;
    }
    
    pid = fork();
    
    if (pid == 0) {
        // 子进程 - 读取端
        close(pipe_fd[1]);  // 关闭写端
        
        char buffer[256];
        ssize_t bytes_read;
        
        std::cout << "子进程等待接收数据..." << std::endl;
        
        while ((bytes_read = read(pipe_fd[0], buffer, sizeof(buffer) - 1)) > 0) {
            buffer[bytes_read] = '\0';
            std::cout << "子进程接收到: " << buffer << std::endl;
        }
        
        close(pipe_fd[0]);
        exit(0);
        
    } else if (pid > 0) {
        // 父进程 - 写入端
        close(pipe_fd[0]);  // 关闭读端
        
        const char* messages[] = {
            "消息1: Hello",
            "消息2: World",
            "消息3: 进程间通信",
            "消息4: 管道示例"
        };
        
        for (int i = 0; i < 4; ++i) {
            write(pipe_fd[1], messages[i], strlen(messages[i]));
            std::cout << "父进程发送: " << messages[i] << std::endl;
            sleep(1);
        }
        
        close(pipe_fd[1]);  // 关闭写端,子进程会收到EOF
        
        // 等待子进程完成
        wait(nullptr);
        std::cout << "管道通信完成" << std::endl;
        
    } else {
        std::cerr << "Fork失败" << std::endl;
        return 1;
    }
    
    return 0;
}

3. 共享内存

#include <iostream>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstring>
#include <semaphore.h>
#include <fcntl.h>

struct SharedData {
    int counter;
    char message[256];
};

int main() {
    // 创建共享内存
    key_t key = ftok(".", 'S');
    int shm_id = shmget(key, sizeof(SharedData), IPC_CREAT | 0666);
    
    if (shm_id == -1) {
        std::cerr << "共享内存创建失败" << std::endl;
        return 1;
    }
    
    // 附加共享内存
    SharedData* shared_data = (SharedData*)shmat(shm_id, nullptr, 0);
    if (shared_data == (SharedData*)-1) {
        std::cerr << "共享内存附加失败" << std::endl;
        return 1;
    }
    
    // 初始化共享数据
    shared_data->counter = 0;
    strcpy(shared_data->message, "初始消息");
    
    // 创建信号量用于同步
    sem_t* sem = sem_open("/shared_mem_sem", O_CREAT, 0644, 1);
    if (sem == SEM_FAILED) {
        std::cerr << "信号量创建失败" << std::endl;
        return 1;
    }
    
    const int num_processes = 3;
    
    for (int i = 0; i < num_processes; ++i) {
        pid_t pid = fork();
        
        if (pid == 0) {
            // 子进程
            for (int j = 0; j < 5; ++j) {
                // 获取信号量
                sem_wait(sem);
                
                // 修改共享数据
                shared_data->counter++;
                sprintf(shared_data->message, "进程%d-更新%d", i, j);
                
                std::cout << "进程 " << i << " (PID: " << getpid() 
                          << ") 更新计数器: " << shared_data->counter 
                          << ", 消息: " << shared_data->message << std::endl;
                
                // 释放信号量
                sem_post(sem);
                
                sleep(1);
            }
            
            exit(0);
        }
    }
    
    // 父进程等待所有子进程完成
    for (int i = 0; i < num_processes; ++i) {
        wait(nullptr);
    }
    
    std::cout << "最终计数器值: " << shared_data->counter << std::endl;
    std::cout << "最终消息: " << shared_data->message << std::endl;
    
    // 清理资源
    shmdt(shared_data);
    shmctl(shm_id, IPC_RMID, nullptr);
    sem_close(sem);
    sem_unlink("/shared_mem_sem");
    
    return 0;
}

4. 消息队列

#include <iostream>
#include <sys/msg.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstring>

struct Message {
    long msg_type;
    char msg_text[256];
};

void sender_process(int msg_queue_id) {
    Message msg;
    
    for (int i = 1; i <= 5; ++i) {
        msg.msg_type = i;
        sprintf(msg.msg_text, "消息 %d 来自发送进程 (PID: %d)", i, getpid());
        
        if (msgsnd(msg_queue_id, &msg, sizeof(msg.msg_text), 0) == -1) {
            std::cerr << "消息发送失败" << std::endl;
            return;
        }
        
        std::cout << "发送: " << msg.msg_text << std::endl;
        sleep(1);
    }
}

void receiver_process(int msg_queue_id, long msg_type) {
    Message msg;
    
    while (true) {
        if (msgrcv(msg_queue_id, &msg, sizeof(msg.msg_text), msg_type, 0) == -1) {
            std::cerr << "消息接收失败" << std::endl;
            break;
        }
        
        std::cout << "接收进程 (类型 " << msg_type << ") 收到: " 
                  << msg.msg_text << std::endl;
        
        // 如果收到特定消息则退出
        if (strstr(msg.msg_text, "消息 5") != nullptr) {
            break;
        }
    }
}

int main() {
    // 创建消息队列
    key_t key = ftok(".", 'M');
    int msg_queue_id = msgget(key, IPC_CREAT | 0666);
    
    if (msg_queue_id == -1) {
        std::cerr << "消息队列创建失败" << std::endl;
        return 1;
    }
    
    std::cout << "消息队列创建成功,ID: " << msg_queue_id << std::endl;
    
    // 创建发送进程
    pid_t sender_pid = fork();
    if (sender_pid == 0) {
        sender_process(msg_queue_id);
        exit(0);
    }
    
    // 创建接收进程
    pid_t receiver_pid = fork();
    if (receiver_pid == 0) {
        receiver_process(msg_queue_id, 0);  // 接收所有类型的消息
        exit(0);
    }
    
    // 父进程等待子进程完成
    wait(nullptr);
    wait(nullptr);
    
    // 删除消息队列
    msgctl(msg_queue_id, IPC_RMID, nullptr);
    std::cout << "消息队列通信完成" << std::endl;
    
    return 0;
}

性能对比与选择建议

1. Python GIL的影响

import threading
import multiprocessing
import time
import math

def cpu_bound_task(n):
    """CPU密集型任务"""
    result = 0
    for i in range(n):
        result += math.sqrt(i)
    return result

def io_bound_task(delay):
    """I/O密集型任务"""
    time.sleep(delay)
    return f"完成,延迟{delay}秒"

def compare_performance():
    """性能对比"""
    print("=== Python 多线程 vs 多进程性能对比 ===")
    
    # CPU密集型任务
    tasks = [1000000, 1000000, 1000000, 1000000]
    
    # 单线程
    start = time.time()
    results = [cpu_bound_task(n) for n in tasks]
    single_time = time.time() - start
    print(f"CPU密集型 - 单线程: {single_time:.2f}秒")
    
    # 多线程(受GIL限制)
    start = time.time()
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(cpu_bound_task, tasks))
    thread_time = time.time() - start
    print(f"CPU密集型 - 多线程: {thread_time:.2f}秒")
    
    # 多进程
    start = time.time()
    with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(cpu_bound_task, tasks))
    process_time = time.time() - start
    print(f"CPU密集型 - 多进程: {process_time:.2f}秒")
    
    print(f"多进程加速比: {single_time / process_time:.2f}x")
    
    # I/O密集型任务
    print("\n=== I/O密集型任务 ===")
    io_tasks = [1, 1, 1, 1]
    
    # 单线程
    start = time.time()
    results = [io_bound_task(delay) for delay in io_tasks]
    single_io_time = time.time() - start
    print(f"I/O密集型 - 单线程: {single_io_time:.2f}秒")
    
    # 多线程
    start = time.time()
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(io_bound_task, io_tasks))
    thread_io_time = time.time() - start
    print(f"I/O密集型 - 多线程: {thread_io_time:.2f}秒")
    
    print(f"多线程加速比: {single_io_time / thread_io_time:.2f}x")

if __name__ == "__main__":
    compare_performance()

2. 选择建议

场景PythonC/C++推荐方案
CPU密集型多进程多线程/多进程Python: multiprocessing
C++: std::thread
I/O密集型多线程多线程Python: threading
C++: std::thread
大量并发asyncioepoll/kqueue异步编程
实时性要求高C/C++C/C++C++ std::thread
开发效率优先PythonPythonPython threading/multiprocessing

3. 最佳实践

# Python 最佳实践示例
import concurrent.futures
import threading
import multiprocessing
import queue
import time

class TaskManager:
    def __init__(self):
        self.cpu_executor = concurrent.futures.ProcessPoolExecutor(
            max_workers=multiprocessing.cpu_count()
        )
        self.io_executor = concurrent.futures.ThreadPoolExecutor(
            max_workers=20  # I/O密集型可以有更多线程
        )
    
    def submit_cpu_task(self, func, *args, **kwargs):
        """提交CPU密集型任务"""
        return self.cpu_executor.submit(func, *args, **kwargs)
    
    def submit_io_task(self, func, *args, **kwargs):
        """提交I/O密集型任务"""
        return self.io_executor.submit(func, *args, **kwargs)
    
    def shutdown(self):
        """关闭执行器"""
        self.cpu_executor.shutdown(wait=True)
        self.io_executor.shutdown(wait=True)

# 使用示例
def cpu_task(n):
    return sum(i*i for i in range(n))

def io_task(url):
    time.sleep(1)  # 模拟网络请求
    return f"Downloaded {url}"

def main():
    manager = TaskManager()
    
    # 提交任务
    cpu_futures = [manager.submit_cpu_task(cpu_task, 100000) for _ in range(4)]
    io_futures = [manager.submit_io_task(io_task, f"url{i}") for i in range(10)]
    
    # 获取结果
    for future in concurrent.futures.as_completed(cpu_futures + io_futures):
        result = future.result()
        print(f"任务完成: {result}")
    
    manager.shutdown()

if __name__ == "__main__":
    main()

总结

关键要点

  1. Python:

    • 多线程适合I/O密集型任务
    • 多进程适合CPU密集型任务
    • GIL限制了多线程的CPU并行性
  2. C/C++:

    • 真正的并行执行
    • 需要手动管理同步和内存
    • 性能更高但复杂度更大
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值