码迷,mamicode.com
首页 > 编程语言 > 详细

Semaphore built from mutex in C++11

时间:2015-03-04 14:17:27      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

#include <mutex>
#include <condition_variable>
using namespace std;

class semaphore{
private:
    mutex mtx;
    condition_variable cv;
    int count;

public:
    semaphore(int count_ = 0):count(count_){;}
    void notify()
    {
        unique_lock<mutex> lck(mtx);
        ++count;
        cv.notify_one();
    }
    void wait()
    {
        unique_lock<mutex> lck(mtx);

        while(count == 0){
            cv.wait(lck);
        }
        count--;
    }
};

 

Semaphore built from mutex in C++11

标签:

原文地址:http://www.cnblogs.com/Kai-Xing/p/4312999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!