C++ 多线程并发 基础入门教程 1.3 条件变量(condition_variable), 信号量(semaphore)

该博客通过观看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;
}

在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗吧!骚年!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值