boost mutex condition
来源:互联网 发布:ssh 远程端口转发 编辑:程序博客网 时间:2024/04/28 08:42
//============================================================================
// Name : Test.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : in C++, Ansi-style
//============================================================================
#include <string>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
const unsigned int BUF_SIZE = 10 ;
const unsigned int ITERS = 100 ;
boost::mutex io_mutex ;
class buffer
{
public :
typedef boost::mutex::scoped_lock scoped_lock ;
buffer():usTell(0) ,usHead(0) ,usSize(0)
{
}
void put(int m)
{
scoped_lock lock(mutex) ;
{
scoped_lock lock(io_mutex) ;
std::cout<< "put lock mutex start ," <<std::endl ;
}
if(BUF_SIZE == usSize)
{
{
scoped_lock lock(io_mutex) ;
std::cout<<"Buffer is full .Waiting..." << std::endl;
}
while(BUF_SIZE == usSize)
{
std::cout<<"put wait cond.wait " <<std::endl ;
cond.wait(lock);
}
}
buf[usTell] = m ;
usTell = (usTell+1)%BUF_SIZE ;
++usSize ;
cond.notify_one() ;
{
scoped_lock lock(io_mutex) ;
std::cout<< "put lock mutex end ," <<std::endl ;
}
}
int get()
{
scoped_lock lk(mutex) ;
{
scoped_lock lock(io_mutex) ;
std::cout<< "get lock mutex start ," <<std::endl ;
}
if(0 == usSize)
{
{
scoped_lock lock(io_mutex) ;
std::cout<<"Buffer is empty . Waiting ..." << std::endl;
}
while(0 == usSize)
{
std::cout<<"get full = 0 cond.wait " <<std::endl ;
cond.wait(lk);
}
}
int i = buf[usHead] ;
usHead = (usHead+1)%BUF_SIZE ;
--usSize ;
cond.notify_one() ;
{
scoped_lock lock(io_mutex) ;
std::cout<< "get lock mutex end ," <<std::endl ;
}
return i ;
}
private:
boost::mutex mutex ;
boost::condition cond ;
unsigned int usTell ;
unsigned int usHead ;
unsigned int usSize ;
int buf[BUF_SIZE];
};
buffer buf ;
void writer()
{
for(unsigned int i = 0 ; i < ITERS ; i++)
{
{
boost::mutex::scoped_lock lock(io_mutex) ;
std::cout<<"sending :" <<i <<std::endl;
}
buf.put(i);
}
}
void reader()
{
for(unsigned int i = 0 ; i < ITERS ; i++)
{
int n = buf.get() ;
{
boost::mutex::scoped_lock lock(io_mutex) ;
std::cout<<"received:" << n << std::endl ;
}
}
}
int main()
{
boost::thread thr1(&reader) ;
boost::thread thr2(&writer) ;
thr1.join() ;
thr2.join() ;
std::cout << "hehe \n" ;
return 0;
}
get lock mutex start ,
Buffer is empty . Waiting ...
get full = 0 cond.wait
sending :0put lock mutex start ,
put lock mutex end ,
sending :1
get lock mutex end ,
received:0
put lock mutex start ,
put lock mutex end ,
sending :2
get lock mutex start ,
get lock mutex end ,
received:1
put lock mutex start ,
put lock mutex end ,
sending :3
get lock mutex start ,
get lock mutex end ,
received:2
put lock mutex start ,
put lock mutex end ,
sending :4
get lock mutex start ,
get lock mutex end ,
这是运行的部分结果 , 有点不明白。 第二个锁是怎么进入到第一个锁的。
- boost mutex condition
- boost condition
- boost condition
- boost condition
- boost::condition
- boost mutex
- boost Mutex
- mutex and Condition Variables
- android mutex and condition
- 2 condition & mutex
- pthread condition mutex
- boost之Interprocess库,进程间通讯库之condition/mutex/scoped_lock
- 线程同步:Mutex和Condition
- mutex 和 condition variable[转载]
- mutex和condition的用法
- boost::mutex vs boost::recursive_mutex
- boost::mutex vs boost::recursive_mutex
- boost::mutex vs boost::recursive_mutex
- CCSpriteFrameCache的用法
- 海选女主角
- c#漏斗图表控件
- MSP430的时钟周期、机器周期、指令周期之间的关系
- 如何在C++中struct与Class的区别
- boost mutex condition
- CString类的完美总结
- foreign key 约束(重点)
- 关于IO流
- 由FPS到CreateDevice的渲染结构体参数的理解
- 深入理解Linux之copy_process()
- iOS开发------Navigation Bar的简单设置
- poj 1328 Radar Installation_贪心
- 如何在集群上运行Shark