boost 多线程条件变量
来源:互联网 发布:网络聊天工具 编辑:程序博客网 时间:2024/05/12 06:47
#include <boost/thread/thread.hpp>#include <boost/thread/mutex.hpp>#include <boost/thread/condition.hpp>#include <iostream>const int BUF_SIZE = 10 ;const int ITERS = 100 ;boost::mutex io_mutex ;class buffer{public: buffer() { p = 0 ; c = 0 ; full = 0 ; } void put(int m) { boost::mutex::scoped_lock lk(m_mutex) ; if (full == BUF_SIZE) { { boost::mutex::scoped_lock tp_lk(io_mutex) ; std::cout<<"Buffer is full.Waiting..."<<std::endl ; } while (full == BUF_SIZE) { m_condition.wait(lk) ; } } buf[p] = m ; p = (p + 1)%BUF_SIZE ; ++full ; m_condition.notify_one() ; } int get() { boost::mutex::scoped_lock lk(m_mutex) ; if (full == 0) { { boost::mutex::scoped_lock tp_lk(io_mutex) ; std::cout<<"Buffer is empty.Waiting..."<<std::endl ; } while (full == 0) { m_condition.wait(lk) ; } } int i = buf[c] ; c = (c + 1)%BUF_SIZE ; --full ; m_condition.notify_one() ; return i ; }private: boost::mutex m_mutex ; boost::condition m_condition ; unsigned int p , c , full ; int buf[BUF_SIZE] ;};buffer buf;void writer(){ for (int n = 0; n < ITERS; ++n) { { boost::mutex::scoped_lock lock(io_mutex); std::cout << "sending: " << n << std::endl; } buf.put(n); }}void reader(){ for (int x = 0; x < ITERS; ++x) { int n = buf.get(); { boost::mutex::scoped_lock lock(io_mutex); std::cout << "received: " << n << std::endl; } }}int main(int argc, char* argv[]){ boost::thread thrd1(&reader); boost::thread thrd2(&writer); thrd1.join(); thrd2.join(); return 0;}// #include <boost/thread/thread.hpp>// #include <boost/thread/mutex.hpp>// #include <boost/thread/condition.hpp>// #include <iostream>// const int BUF_SIZE = 10;// const int ITERS = 100;// boost::mutex io_mutex;// class buffer// {// public:// typedef boost::mutex::scoped_lock// scoped_lock;// // buffer()// : p(0), c(0), full(0)// {// }// // void put(int m)// {// scoped_lock lock(mutex);// if (full == BUF_SIZE)// {// {// boost::mutex::scoped_lock// lock(io_mutex);// std::cout <<// "Buffer is full. Waiting..."// << std::endl;// }// while (full == BUF_SIZE)// cond.wait(lock);// }// buf[p] = m;// p = (p+1) % BUF_SIZE;// ++full;// cond.notify_one();// }// // int get()// {// scoped_lock lk(mutex);// if (full == 0)// {// {// boost::mutex::scoped_lock// lock(io_mutex);// std::cout <<// "Buffer is empty. Waiting..."// << std::endl;// }// while (full == 0)// cond.wait(lk);// }// int i = buf[c];// c = (c+1) % BUF_SIZE;// --full;// cond.notify_one();// return i;// }// // private:// boost::mutex mutex;// boost::condition cond;// unsigned int p, c, full;// int buf[BUF_SIZE];// };// buffer buf;// void writer()// {// for (int n = 0; n < ITERS; ++n)// {// {// boost::mutex::scoped_lock// lock(io_mutex);// std::cout << "sending: "// << n << std::endl;// }// buf.put(n);// }// }// void reader()// {// for (int x = 0; x < ITERS; ++x)// {// int n = buf.get();// {// boost::mutex::scoped_lock// lock(io_mutex);// std::cout << "received: "// << n << std::endl;// }// }// }// int main(int argc, char* argv[])// {// boost::thread thrd1(&reader);// boost::thread thrd2(&writer);// thrd1.join();// thrd2.join();// return 0;// }
0 0
- boost 多线程条件变量
- boost多线程编程条件变量
- BOOST库 条件变量--多线程通信机制
- C++ BOOST库 条件变量[多线程通信]机制 简单介绍
- boost条件变量使用
- 【Boost】boost库中thread多线程详解10——condition条件变量
- boost 线程、互斥体、条件变量
- boost条件变量(condition_variable…
- 多线程的条件变量
- Posix多线程-条件变量
- 多线程之条件变量
- 多线程编程-条件变量
- 多线程--条件变量
- 多线程编程: 条件变量
- 多线程的条件变量
- 多线程的条件变量
- 多线程同步条件变量
- 多线程_条件变量
- 深度学习解决多视图非线性数据特征融合问题
- CF
- qt 多核编译+源码调试
- VS 2013+Pcl 1.7.2 安装配置及常见问题
- leetcode-第二周
- boost 多线程条件变量
- 我的笔记
- 华科历年考研上机题整理
- 移动web开发常用的框架(四)
- 13-15 标准IO之fread、fwrite
- 机器学习算法-朴素贝叶斯分类
- 5.freeswitch架构
- windows运行打开服务命令
- Android 开发软件架构思考以及经验总结