温馨提示×

C++ Linux环境下如何并发编程

小樊
39
2025-03-15 00:40:00
栏目: 编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

1. POSIX Threads (pthreads)

POSIX Threads 是一种标准的线程库,适用于大多数Unix-like系统,包括Linux。

基本步骤:

  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);
    
  5. 销毁线程(可选):

    pthread_exit(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();
    
  5. 分离线程(可选):

    t.detach();
    

示例代码:

#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. 并发容器和算法

C++11 及其后续版本还提供了一些并发容器和算法,如 std::atomicstd::mutexstd::lock_guardstd::unique_lock 等,用于实现线程安全的操作。

示例代码:

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

std::mutex mtx;

void print_block(int n, char c) {
    std::lock_guard<std::mutex> guard(mtx);
    for (int i = 0; i < n; ++i) {
        std::cout << c;
    }
    std::cout << '\n';
}

int main() {
    std::thread th1(print_block, 50, '*');
    std::thread th2(print_block, 50, '$');

    th1.join();
    th2.join();

    return 0;
}

通过这些方法,你可以在Linux环境下使用C++进行高效的并发编程。选择哪种方法取决于你的具体需求和应用场景。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:Linux C++编程资源推荐

0