#include "stdafx.h"
#include
#include
#include
#include
const int Max = 20;
boost::mutex mutex;
boost::condition cond;
class product
{
public:
product()
:count(0)
{}
void make();
void take();
int num();
private:
int count;
};
void product::make()
{
count++;
}
void product::take()
{
if(count>0) count--;
}
int product::num()
{
return count;
}
product pro;
void c()
{
{
boost::mutex::scoped_lock lock(mutex);
while(pro.num() }
for(;;){
boost::mutex::scoped_lock lock(mutex);
if(pro.num()>0) {
pro.take();
std::cout<<"decrease: "<< pro.num()<<"\n";
}else {
std::cout<<"decrease end\n"<< "\n";
break;
}
}
}
void p()
{
for(;;){
boost::mutex::scoped_lock lock(mutex);
pro.make();
std::cout<<"increase: "<< pro.num()<< "\n";
if(pro.num()>Max) {
std::cout<<"increase end\n"<< "\n";
cond.notify_all();
break;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread thrd1(&p);
boost::thread thrd2(&p);
boost::thread thrd3(&c);
thrd1.join();
thrd2.join();
thrd3.join();
return 0;
}
阅读(791) | 评论(0) | 转发(0) |