c++并发编程(一)

多线程创建

c++创建线程非常简单,需要用到std的thread类,可用下面的方法来创建一个thread

void threadSafe(weak_ptr pw) {
std::this_thread::sleep_for(std::chrono::seconds(2));
shared_ptr ps = pw.lock();
if(ps != nullptr) {
    ps->show();
}
}

std::thread t1(threadSafe, weak_ptr(p));

注意,thread的构建函数中,两个参数,一个是线程中要执行的方法,另一个则是方法中需要的参数

c++和c中创建线程的方式并不一样,不要搞混淆了

互斥

在RAII一文中说过,c++的互斥加锁,可以使用lock_guard

  • lock_guard在构造函数中加锁,在析构函数中释放锁,这样就不会导致忘记释放或者其它原因导致锁没有释放
  • lock_guard是一种较简单的锁,相比另一种锁unique_lock而言

unique_lock是通用互斥包装器,允许延迟锁定、锁定的有时限尝试、递归锁定、所有权转移和与条件变量一同使用。

注意,要和条件变量一起使用,就只能用unique_lock了。

unique_lock比lock_guard更加灵活,功能更加强大,但unique_lock消耗更多

条件变量

条件变量,condition_variable,有点类似 java 中的wait和notify方法。当 condition_variable调用wait方法时,线程将释放锁并且等待其它线程将其唤醒,当另一个线程也调用这个condition_variable对象的notify_one或者notify_all方法时,前一个线程将有可能被唤醒

虚假唤醒

在正常情况下,wait类型函数返回时要不是因为被唤醒,要不是因为超时才返回,但是在实际中发现,因此操作系统的原因,wait类型在不满足条件时,它也会返回,这就导致了虚假唤醒。因此,我们一般都是使用带有谓词参数的wait函数,因为这种(xxx, Predicate pred )类型的函数等价于:

while (!pred()) //while循环,解决了虚假唤醒的问题
{
    wait(lock);
}

所以为了防止这种虚假唤醒,一般有两个方法,一个方法就是上文中的while条件循环,另一种是:

cv.wait(lcx, []{return !que.empty();});

最后我们用一个生产者消费者模型来说明线程生成,加锁,及条件变量使用等

std::mutex mtx;
std::queue que;
std::condition_variable cv;
int cnt = 1;

void producer2(int id){
while (true){
    std::unique_lock lcx(mtx);
    que.push(cnt);
    this_thread::sleep_for(10ms);
    cout << "produce " << id << "  向队列中添加了数据" << cnt ++ << endl;
    cv.notify_one();
}
}

void consumer2(int id){
while (true){
    std::unique_lock lcx(mtx);
    cv.wait(lcx, []{return !que.empty();});
    int temp = que.front();
    cout << "consume " << id << "  -----从队列中取出数据:" << temp << endl;
    que.pop();
}
}

void test_produce_consume(){
std::thread thd1[2], thd2[2];
for (int i = 0; i < 2; i++) {
    cout << "create thread i = " << i << endl;
    thd1[i] = std::thread(producer2, i);
    thd2[i] = std::thread(consumer2, i);
}
for (int i = 0; i < 2; i++) {
    thd1[i].join();
    thd2[i].join();
}
}

你可能感兴趣的:(c++并发编程(一))