boost多线程编程条件变量
来源:互联网 发布:如何在知乎专栏写文章 编辑:程序博客网 时间:2024/04/28 10:28
#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_lockscoped_lock;buffer(): p(0), c(0), full(0){}void put(int m){scoped_lock lock(mutex);if (full == BUF_SIZE){{boost::mutex::scoped_locklock(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_locklock(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_locklock(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_locklock(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库 条件变量--多线程通信机制
- Linux多线程编程-条件变量
- linux多线程编程---条件变量
- 多线程编程-条件变量pthread_cond_t
- Linux多线程编程-条件变量
- Linux多线程编程---条件变量
- c++ 多线程编程 条件变量
- c++的boost库多线程(Thread)编程(线程操作,互斥体mutex,条件变量)详解
- c++的boost库多线程(Thread)编程(线程操作,互斥体mutex,条件变量)详解
- boost库多线程(Thread)编程(线程操作,互斥体mutex,条件变量)
- C++ BOOST库 条件变量[多线程通信]机制 简单介绍
- Linux编程练习 --多线程4--条件变量
- Linux多线程编程下的条件变量
- 毕业设计之带有ajax的function()函数返回值问题
- 分页实现后端逻辑
- SaaS营销网站的剖析之案例二——Wistia
- 关于eclipse安装android开发环境细节
- 如何使用 50 行 Python 代码制作一个计算器
- boost多线程编程条件变量
- R语言︱情感分析—词典型代码实践(最基础)(一)
- 苹果Xcode帮助文档阅读指南
- Windows自动更新API (4)
- XMG 调用UIWindow hitTest方法
- 城市定位
- Android Studio JNI/NDK 编程(二) Windows 下环境搭建 demo 开发
- 浅谈DIP和IOC
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) – 整理