温馨提示×

C++在Linux怎样进行并发编程

小樊
54
2025-07-28 06:15:02
栏目: 编程语言

在Linux下使用C++进行并发编程,主要有以下几种方式:

1. POSIX Threads (pthreads)

POSIX Threads 是一种标准的线程库,可以在大多数类Unix系统上使用。

基本步骤:

  1. 包含头文件

    #include <pthread.h>
    
  2. 定义线程函数

    void* thread_function(void* arg) {
        // 线程执行的代码
        return NULL;
    }
    
  3. 创建线程

    pthread_t thread_id;
    int result = pthread_create(&thread_id, NULL, thread_function, NULL);
    if (result != 0) {
        // 处理错误
    }
    
  4. 等待线程结束

    pthread_join(thread_id, NULL);
    

示例代码:

#include <iostream>
#include <pthread.h>

void* thread_function(void* arg) {
    std::cout << "Thread is running" << std::endl;
    return NULL;
}

int main() {
    pthread_t thread_id;
    int result = pthread_create(&thread_id, NULL, thread_function, NULL);
    if (result != 0) {
        std::cerr << "Error creating thread" << std::endl;
        return 1;
    }
    pthread_join(thread_id, NULL);
    std::cout << "Thread finished" << std::endl;
    return 0;
}

2. C++11 标准库线程

C++11 引入了标准库线程支持,提供了更现代和易用的接口。

基本步骤:

  1. 包含头文件

    #include <thread>
    
  2. 定义线程函数

    void thread_function() {
        // 线程执行的代码
    }
    
  3. 创建线程

    std::thread t(thread_function);
    
  4. 等待线程结束

    t.join();
    

示例代码:

#include <iostream>
#include <thread>

void thread_function() {
    std::cout << "Thread is running" << std::endl;
}

int main() {
    std::thread t(thread_function);
    t.join();
    std::cout << "Thread finished" << std::endl;
    return 0;
}

3. 异步任务(std::async)

std::async 提供了一种更高级的方式来执行异步任务,它会自动管理线程的创建和销毁。

基本步骤:

  1. 包含头文件

    #include <future>
    
  2. 定义任务函数

    int task_function() {
        // 任务执行的代码
        return 42;
    }
    
  3. 启动异步任务

    std::future<int> result = std::async(std::launch::async, task_function);
    
  4. 获取任务结果

    int value = result.get();
    

示例代码:

#include <iostream>
#include <future>

int task_function() {
    std::cout << "Task is running" << std::endl;
    return 42;
}

int main() {
    std::future<int> result = std::async(std::launch::async, task_function);
    std::cout << "Waiting for task to finish..." << std::endl;
    int value = result.get();
    std::cout << "Task finished with result: " << value << std::endl;
    return 0;
}

4. 条件变量和互斥锁

在多线程编程中,条件变量和互斥锁是常用的同步机制。

示例代码:

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool ready = false;

void worker_thread() {
    std::unique_lock<std::mutex> lock(mtx);
    cv.wait(lock, []{ return ready; });
    std::cout << "Worker thread is processing data" << std::endl;
}

void trigger_work() {
    std::lock_guard<std::mutex> lock(mtx);
    ready = true;
    cv.notify_one();
}

int main() {
    std::thread worker(worker_thread);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    trigger_work();
    worker.join();
    return 0;
}

总结

  • pthreads:适用于需要精细控制线程的场景。
  • C++11 标准库线程:提供了更现代和易用的接口。
  • std::async:适用于需要自动管理线程的场景。
  • 条件变量和互斥锁:用于线程间的同步。

选择哪种方式取决于具体的需求和场景。

0