该博客通过观看b站视频 [C++ 多线程并发 基础入门教程] 基础入门教程 1.3 条件变量(condition_variable), 信号量(semaphore) 进行的笔记总结,up主讲的非常好,适合了解一点多线程知识,想快速入门的同学
目录
使用总结:
使用 | 解释 |
---|---|
condition_variable 条件变量 | 通过wait()阻塞线程,使用notify_one()或者notify_all()来解除1个或者全部等待的条件变量 |
binary_semaphore 条件信号量 | 通过acquire()请求一个信号量(阻塞), release释放一个信号量,优势比condition_variable 条件变量性能更好 |
counting_semaphore 计数信号量 | 用来控制能够同时访问某特定资源的活动数目,可以通过acquire()请求一个信号量(阻塞), release(x)释放多个信号量,来使多个线程进行运转 |
引出问题(while循环很占CPU)
如果单独一个while循环会让cpu达到百分之10
使用std::this_thread::sleep_for会缓解循环,但是不好确定大小
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <deque>
std::mutex mtx;
std::deque<int> q;
// producer
void task1()
{
int i = 0;
while (true)
{
std::unique_lock<std::mutex> lock(mtx);
q.push_back(i);
//std::this_thread::sleep_for(std::chrono::microseconds(10));
if (i < 99999999)
i++;
else
i = 0;
}
}
// consumer
void task2()
{
int data = 0;
while (true)
{
std::unique_lock<std::mutex> lock(mtx);
if (!q.empty())
{
data = q.front();
q.pop_front();
std::cout << "Get value from que:" << data << std::endl;
}
//std::this_thread::sleep_for(std::chrono::microseconds(10));
}
}
int main()
{
std::thread t1(task1);
std::thread t2(task2);
t1.join();
t2.join();
return 0;
}
方案一:使用 condition_variable 条件变量
condition_variable中
cv.wait()会一直等待,直到cv.notify_one()或者cv.notify_all()才会唤醒
缺点:只能唤醒一个或者全部
cv.wait(lock);相当于lock.unlock(); cv.wait();两步操作
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <deque>
#include <condition_variable>
std::mutex mtx;
std::deque<int> q;
std::condition_variable cv;
// producer
void task1()
{
int i = 0;
while (true)
{
std::unique_lock<std::mutex> lock(mtx);
cv.notify_one(); // 唤醒某一个等待变量的等待线程
//cv.notify_all(); // 唤醒所有等待变量的等待线程
q.push_back(i);
if (i < 99999999)
i++;
else
i = 0;
}
}
// consumer
void task2()
{
int data = 0;
while (true)
{
std::unique_lock<std::mutex> lock(mtx);
if (q.empty())
{
cv.wait(lock);
//lock.unlock();
//cv.wait();
}
if (!q.empty())
{
data = q.front();
q.pop_front();
std::cout << "Get value from que:" << data << std::endl;
}
}
}
int main()
{
std::thread t1(task1);
std::thread t2(task2);
t1.join();
t2.join();
return 0;
}
方案二:使用 semaphore信号量
首先信号量是C++20才有的特性,需要保证标准是C++20
使用 binary_semaphore 条件信号量
binary_semaphore信号量实际就是计数信号量中只有一个信号量的特例
通过acquire请求一个信号量,release释放一个信号量
binary_semaphore条件信号量比condition_variable 条件变量性能更好推荐使用条件信号量
#include <iostream>
#include <thread>
#include <mutex>
#include <semaphore>
//std::counting_semaphore<2> csem(0);
std::binary_semaphore bsem(0);
void task()
{
std::cout << "ready to recv signal: acquire\n";
bsem.acquire();
std::cout << "acquire end\n";
}
int main()
{
std::thread t(task);
std::cout << "read to signal: release\n";
bsem.release();
std::cout << "signla end\n";
t.join();
return 0;
}
使用 counting_semaphore 计数信号量
通过std::counting_semaphore<3>使其最大信号量为3,acquire来请求信号量
csem.release(2)来释放两个信号量
#include <iostream>
#include <thread>
#include <mutex>
#include <semaphore>
std::counting_semaphore<3> csem(0);
//std::binary_semaphore bsem(0);
void task()
{
std::cout << "ready to recv signal: acquire\n";
csem.acquire();
std::cout << "acquire end\n";
}
int main()
{
std::thread t0(task);
std::thread t1(task);
std::thread t2(task);
std::thread t3(task);
std::thread t4(task);
std::cout << "read to signal: release\n";
csem.release(2);
std::cout << "signla end\n";
t0.join();
t1.join();
t2.join();
t3.join();
t4.join();
return 0;
}