pthread和std::thread对比

std::thread是C++11接口,pthread是C++98接口且只支持Linux。

示例:

pthread_create(&thread, &attr, f, static_cast<void *>(&args));
// 其中f是函数,args是所有参数打包成的结构体。因为pthread_create的第四个参数类型是void*,所以需要强制转型
//pthread_create只接受void *f(void *)这样的函数签名
// 假设buy是Consumer的一个可调用的成员函数
// Clara会去执行Consumer.buy(phone)
Consumer Clara;
std::thread action(buy, Clara, phone);

Mutex

pthread_mutex_lock(&mutex)
{
    std::lock_guard<std::mutex> guard(mutex); // 加锁
    ...
    // 自动解锁
}

Condition variable

std::condition_variable condvar;
 
consumer:
        std::unique_lock<std::mutex> ulock(mutex);
        condvar.wait(ulock, []{ return msgQueue.size() > 0;});
 
producer:
        condvar.notify_all();

C++11增加future和atomic


#include <chrono>
#include <iostream>
#include <future>


future f = async(launch::async, {
    std::chrono::milliseconds dura(2000);
    std::this_thread::sleep_for(dura);
    return 0;
});

atomic位于头文件atomic下,有些时候你也可以用它来替换掉Lock(假如整个race condition中只有单个变量)。

std::thread对比于pthread的优缺点:

优点:
1. 简单,易用
2. 跨平台,pthread只能用在POSIX系统上(其他系统有其独立的thread实现)
3. 提供了更多高级功能,比如future
4. 更加C++(跟匿名函数,std::bind,RAII等C++特性更好的集成)

缺点:
1. 没有RWlock。有一个类似的shared_mutex,不过它属于C++14,你的编译器很有可能不支持。
2. 操作线程和Mutex等的API较少。毕竟为了跨平台,只能选取各原生实现的子集。如果你需要设置某些属性,需要通过API调用返回原生平台上的对应对象,再对返回的对象进行操作。

生产者多消费者

pthread版本

#include <pthread.h>
#include <queue>
#include <stdio.h>
#include <unistd.h>
 
// 注意pthread_*函数返回的异常值
 
pthread_mutex_t mutex;
pthread_cond_t condvar;
 
std::queue<int> msgQueue;
struct Produce_range {
    int start;
    int end;
};
 
void *producer(void *args)
{
    int start = static_cast<Produce_range *>(args)->start;
    int end = static_cast<Produce_range *>(args)->end;
    for (int x = start; x < end; x++) {
        usleep(200 * 1000);
        pthread_mutex_lock(&mutex);
        msgQueue.push(x);
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&condvar);
        printf("Produce message %d\n", x);
    }
    pthread_exit((void *)0);
    return NULL;
}
 
void *consumer(void *args)
{
    int demand = *static_cast<int *>(args);
    while (true) {
        pthread_mutex_lock(&mutex);
        while (msgQueue.size() <= 0) {
            pthread_cond_wait(&condvar, &mutex);
        }
        if (msgQueue.size() > 0) {
            printf("Consume message %d\n", msgQueue.front());
            msgQueue.pop();
            --demand;
        }
        pthread_mutex_unlock(&mutex);
        if (!demand) break;
    }
    pthread_exit((void *)0);
    return NULL;
}
 
 
int main()
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&condvar, NULL);
 
    pthread_t producer1, producer2, producer3, consumer1, consumer2;
 
    Produce_range range1 = {0, 10};
    pthread_create(&producer1, &attr, producer, static_cast<void *>(&range1));
    Produce_range range2 = {range1.end, range1.end + 10};
    pthread_create(&producer2, &attr, producer, static_cast<void *>(&range2));
    Produce_range range3 = {range2.end, range2.end + 10};
    pthread_create(&producer3, &attr, producer, static_cast<void *>(&range3));
 
    int consume_demand1 = 20;
    int consume_demand2 = 10;
    pthread_create(&consumer1, &attr, consumer, 
            static_cast<void *>(&consume_demand1));
    pthread_create(&consumer2, &attr, consumer, 
            static_cast<void *>(&consume_demand2));
 
    pthread_join(producer1, NULL);
    pthread_join(producer2, NULL);
    pthread_join(producer3, NULL);
    pthread_join(consumer1, NULL);
    pthread_join(consumer2, NULL);
} 

std::thread版本

#include <chrono>
#include <condition_variable>
#include <future>
#include <mutex>
#include <queue>
 
// 注意某些调用可能会抛出std::system_error
std::mutex mutex;
std::condition_variable condvar;
 
std::queue<int> msgQueue;
 
void producer(int start, int end)
{
    for (int x = start; x < end; x++) {
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        {        
            std::lock_guard<std::mutex> guard(mutex);
            msgQueue.push(x);
        }
        printf("Produce message %d\n", x);
        condvar.notify_all();
    }
}
 
void consumer(int demand)
{
    while (true) {
        std::unique_lock<std::mutex> ulock(mutex);
        condvar.wait(ulock, []{ return msgQueue.size() > 0;});
        // wait的第二个参数使得显式的double check不再必要
        printf("Consume message %d\n", msgQueue.front());
        msgQueue.pop();
        --demand;
        if (!demand) break;
    }
}
 
 
int main()
{
    std::thread producer1(producer, 0, 10);
    std::thread producer2(producer, 10, 20);
    std::thread producer3(producer, 20, 30);
    std::thread consumer1(consumer, 20);
    std::thread consumer2(consumer, 10);
 
    producer1.join();
    producer2.join();
    producer3.join();
    consumer1.join();
    consumer2.join();
} 

 

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pthreadstd::thread是用于实现多线程编程的API。 pthread是一个用于C语言的多线程库,它提供了一些函数来创建、管理和同步线程,以及操作线程的属性和调度等功能。在使用pthread时,需要手动管理线程的创建、销毁和同步等操作。 而std::thread是C++11标准库提供的多线程支持,它提供了一个高层的线程类,简化了线程的创建和管理过程。使用std::thread可以更方便地创建线程、传递参数、等待线程完成等。与pthread相比,std::thread更加面向对象化,可以更好地与C++的其他特性进行整合。 虽然std::thread在C++11标准库中已经提供了对线程的支持,但在一些特定情况下,可能仍然需要使用底层的线程库,如Windows线程或Pthread。在这种情况下,可以使用SuspendThread函数来控制线程的执行。 总的来说,pthread是C语言的多线程库,而std::thread是C++的多线程支持。它们都可以用来进行多线程编程,选择使用哪种方式主要取决于编程语言的选择和具体需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [从 pthread 转换到 std::thread](https://blog.csdn.net/weixin_33916256/article/details/89679279)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [SuspendThread:暂停线程 v0.1.0](https://download.csdn.net/download/weixin_42117267/19358247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值