码迷,mamicode.com
首页 > 其他好文 > 详细

boost 轻量级信号量

时间:2014-07-01 22:03:34      阅读:474      评论:0      收藏:0      [点我收藏+]

标签:os   io   type   div   ar   re   

#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>    
#include <boost/thread/lock_types.hpp>
#include <boost/thread.hpp> 
 
class semaphore
{
    unsigned int count_;
    boost::mutex mutex_;
    boost::condition_variable condition_;
 
public:
    explicit semaphore(unsigned int initial) : count_(initial){}
 
    void signal()
    {
        {
            boost::lock_guard<boost::mutex> lock(mutex_);
            ++count_;
        }
        condition_.notify_one(); 
    }
 
    void wait() 
    {
        boost::unique_lock<boost::mutex> lock(mutex_);
        while (count_ == 0)
        {
             condition_.wait(lock);
        }
        --count_;
    }
};

boost 轻量级信号量,布布扣,bubuko.com

boost 轻量级信号量

标签:os   io   type   div   ar   re   

原文地址:http://www.cnblogs.com/lidabo/p/3816704.html

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