https://img-blog.csdnimg.cn/8d82d2d76d5942dc91f5a46f8682845e.png

学习如逆水行舟,不进则退!

1.3-多线程控制的另一种姿势-条件变量(condition_variable), 信号量(semaphore)

条件变量(C++11) 为什么要引入条件变量 我们先来看看一个由互斥量加锁构成的生产者消费者模型: // // Created by Alone on 2022-3-27. // #include <iostream>#include <mutex>#include <deque>#include <thread>std::mutex mtx; std::deque<int> q; // producer void task1(){ int i =

1.2-线程安全的保证——互斥量mutex(锁)和原子变量atomic

资源竞争引发的线程安全问题 有如下的代码: #include<thread>#include<iostream>int globalVariable = 0; void task(){ for (int i = 0; i < 1000000; ++i) { ++globalVariable; } } int main(){ std::thread th1(task); std::thread th2(task); th1.join(); th2.join(); std::cout<<globalVariable; } 我们开了两个线程,一共执行了两次 task ,按理来