pthread和std::thread对比

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

示例:

pthread_create(&thread, &attr, f, static_cast(&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 guard(mutex); // 加锁
    ...
    // 自动解锁
}

Condition variable

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

C++11增加future和atomic


#include 
#include 
#include 


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 
#include 
#include 
#include 
 
// 注意pthread_*函数返回的异常值
 
pthread_mutex_t mutex;
pthread_cond_t condvar;
 
std::queue msgQueue;
struct Produce_range {
    int start;
    int end;
};
 
void *producer(void *args)
{
    int start = static_cast(args)->start;
    int end = static_cast(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(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(&range1));
    Produce_range range2 = {range1.end, range1.end + 10};
    pthread_create(&producer2, &attr, producer, static_cast(&range2));
    Produce_range range3 = {range2.end, range2.end + 10};
    pthread_create(&producer3, &attr, producer, static_cast(&range3));
 
    int consume_demand1 = 20;
    int consume_demand2 = 10;
    pthread_create(&consumer1, &attr, consumer, 
            static_cast(&consume_demand1));
    pthread_create(&consumer2, &attr, consumer, 
            static_cast(&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 
#include 
#include 
#include 
#include 
 
// 注意某些调用可能会抛出std::system_error
std::mutex mutex;
std::condition_variable condvar;
 
std::queue 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 guard(mutex);
            msgQueue.push(x);
        }
        printf("Produce message %d\n", x);
        condvar.notify_all();
    }
}
 
void consumer(int demand)
{
    while (true) {
        std::unique_lock 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();
} 

 

你可能感兴趣的:(C++,pthread,std::thread)