C++ 多线程之条件变量

使用条件变量condition_variable 进行程序的休眠与唤醒
notify_one
notify_all

程序示例

#include <condition_variable>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>

/*
condition_variable 条件变量的使用
notify_one一次只能激活一个等待线程
notify_all可以激活所有等待线程
*/
using namespace std;
std::deque<int> q;
std::mutex que_mutex;
std::condition_variable contion;

//线程是往队列里添加数据
void funtion_1() {
  int count = 10;
  while (count > 0) {
    std::unique_lock<mutex> locker(que_mutex);
    q.push_front(count);
    locker.unlock();
    //唤醒等待线程,只能激活一个线程
    //与此相对应的是 notify_all,可以激活所有等待线程
    //contion.notify_one();
    std::this_thread::sleep_for(chrono::seconds(1));
    count--;
  }
}

void function_2() {
  int data = 0;
  while (data != 1) {
    std::unique_lock<mutex> locker(que_mutex);
    //解锁后等待,一个线程不会在被锁住的情况下休眠,
	//wait函数会解锁互斥对象mu,并休眠,休眠后,重新加锁
    //由于需要重复加锁解锁,故这里只能使用 unique_lock 不能使用lock_guard
    contion.wait(locker);
    //也可能被自己激活,即伪激活
    //添加第二个参数 lamba函数
    //contion.wait(locker, []({ return !q.empty() });

    data = q.back();
    q.pop_back();
    locker.unlock();
    std::cout << "thread 2 get a value from t1: " << data << std::endl;
  }
}

void function_3() {
  int data = 0;
  while (data != 1) {
    std::unique_lock<mutex> locker(que_mutex);
    if (!q.empty()) {
      data = q.back();
      q.pop_back();
      locker.unlock();
      std::cout << "thread 2 get a value from t1: " << data << std::endl;

    }
    //如果使用条件变量的话,就不需要下面这些
    //按照下面的写法会有很多无畏的休眠循环
    else {
      locker.unlock();
      std::this_thread::sleep_for(chrono::seconds(10));
    }
  }
}

int main() {
  std::thread t1(funtion_1);
  std::thread t2(function_2);
  t1.join();
  t2.join();
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你想了解关于 C++ 条件变量多线程中的使用。 在多线程中,条件变量通常用于线程间的同步,它可以让一个线程等待另一个线程的特定事件发生。当这个特定事件发生时,另一个线程可以通过条件变量来通知等待的线程,让它继续执行。 C++ 中的条件变量一般需要和互斥锁一起使用,以保证线程安全。当一个线程等待条件变量时,它会释放互斥锁,让其他线程可以继续执行,等到条件变量被唤醒后,线程会重新获得互斥锁并继续执行。 下面是一个简单的示例代码: ```cpp #include <iostream> #include <thread> #include <mutex> #include <condition_variable> std::mutex mtx; std::condition_variable cv; bool ready = false; void worker_thread() { // do some work { std::lock_guard<std::mutex> lk(mtx); ready = true; } cv.notify_one(); // 通知等待的线程 } int main() { std::thread worker(worker_thread); // 等待条件变量 { std::unique_lock<std::mutex> lk(mtx); cv.wait(lk, []{ return ready; }); // 等待条件变量被唤醒 } std::cout << "Worker thread finished\n"; worker.join(); return 0; } ``` 在这个示例中,我们新开了一个线程 `worker_thread`,它会在一段时间后设置条件变量 `ready` 为 `true`,然后通知等待的线程。在 `main` 函数中,我们首先获得了互斥锁 `mtx`,然后调用 `cv.wait` 等待条件变量被唤醒。在等待期间,线程会释放互斥锁,让 `worker_thread` 可以继续执行。等到 `worker_thread` 设置了条件变量并通知后,`main` 函数会重新获得互斥锁并继续执行。最后,我们等待 `worker_thread` 结束并回收资源。 希望这个示例可以帮助你理解条件变量多线程中的使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值