zoukankan      html  css  js  c++  java
  • 有了互斥量为什么还要条件变量?

    互斥量已经可以保证线程的同步,那为什么还要弄条件变量?

    其实答案很简单,条件变量可以在线程没必要执行的时候阻塞住,降低CPU的占用率。

    用代码来测试下

    互斥量

    #include <mutex>
    #include <iostream>
    #include <queue>
    #include <thread>
    
    using namespace std;
    
    queue<int> msgs;
    mutex m;
    condition_variable cond;
    long long loops = 0;
    
    void writerfunc()
    {
        for (int i = 0; i < 5; i++)
        {
            this_thread::sleep_for(chrono::seconds(1));
            lock_guard<mutex> lck(m);
            cout << "Write Message: " << i << endl;
            msgs.push(i);
        }
    }
    
    
    void readerfunc()
    {
        while (true)
        {
            loops++;
            lock_guard<mutex> lck(m);
            if (!msgs.empty())
            {
                int s = msgs.front();
                cout << "Read Message: " << s << endl;
                msgs.pop();
                if (s == 4)
                    break;
            }
        }
    }
    
    
    int main()
    {
        thread reader(readerfunc);
        thread writer(writerfunc);
        writer.join();
        reader.join();
        cout << "轮询次数:" << loops << endl;
        return 0;
    }
    

     输出结果:

     可以看出来,使用互斥量后线程轮训次数非常非常非常多

    条件变量

    #include <mutex>
    #include <iostream>
    #include <queue>
    #include <thread>
    
    using namespace std;
    
    queue<int> msgs;
    mutex m;
    condition_variable cond;
    long long loops = 0;
    
    void writerfunc()
    {
        for (int i = 0; i < 5; i++)
        {
            this_thread::sleep_for(chrono::seconds(1));
            unique_lock<mutex> lck(m);
            cout << "Write Message: " << i << endl;
            msgs.push(i);
            cond.notify_one();
        }
    }
    
    void readerfunc()
    {
        while (true)
        {
            loops++;
            unique_lock<mutex> lck(m);
            cond.wait(lck);
            if (!msgs.empty())
            {
                int s = msgs.front();
                cout << "Read Message: " << s << endl;
                msgs.pop();
                if (s == 4)
                    break;
            }
        }
    }
    
    int main()
    {
        thread reader(readerfunc);
        thread writer(writerfunc);
        writer.join();
        reader.join();
        cout << "轮询次数:" << loops << endl;
        return 0;
    }

    输出结果:

     可以看出,使用条件变量轮询次数显著下降,可以有效降低CPU的占用率。

  • 相关阅读:
    notepad++ 安装
    Git 安装
    C 字符串常量 数据类型
    vue路由传参query和params的区别
    mysql 在 centos 上安装,做成服务,且开机启动 步骤
    全网最详细Apache Kylin1.5安装(单节点)和测试案例 ---> 现在看来 kylin 需要 安装到Hadoop Master 节点上
    Kylin build cube step 2 报错(Kylin 安装在slave 节点上)
    Kylin build cube step 2 报错
    Kylin 环境 搭建 成功
    Kylin 环境 搭建 报错
  • 原文地址:https://www.cnblogs.com/fensi/p/13123974.html
Copyright © 2011-2022 走看看