boost综合使用<function,bind,thread,mutex,condition_variable,shared_ptr>
来源:互联网 发布:php人才招聘网站源码 编辑:程序博客网 时间:2024/05/09 14:51
该例子的功能是:
1、创建测试线程
2、创建工作线程
3、使用list队列
4、线程通知
5、线程锁
工作线程如果没有活要做,则挂起,如果消息队列里有新的消息了,则通知工作线程开始干活。说多了都是废话,上代码:
// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "boost/thread.hpp"#include "boost/thread/condition_variable.hpp"#include "boost/thread/mutex.hpp"#include "boost/shared_ptr.hpp"#include "boost/function.hpp"#include "boost/bind.hpp"#include <list>#include <iostream>boost::shared_ptr<std::list<int>> _g_list;boost::mutex _g_mutex_lock_list;boost::condition_variable _g_cv;boost::mutex _g_cv_mutex;/* *\brief *t: *f: no quit */bool _g_is_quit = false;void Add(int value){{boost::mutex::scoped_lock lock(_g_mutex_lock_list);if (_g_list){_g_list->push_front(value);}}_g_cv.notify_one();}void CreateThr(int threadCnt){if (!threadCnt){// no threadreturn;}// thread functionboost::function<void(void)> func = [](){int cnt = 10;while (--cnt){Add(cnt);}};for (size_t i = 0; i < (size_t)threadCnt; ++i){boost::thread th(boost::bind(func));//boost::thread th(func);}}int GetValueFromList(){boost::mutex::scoped_lock lock(_g_mutex_lock_list);if (!_g_list->size()){return 0;}int v = _g_list->back();_g_list->pop_back();return v;}// first launch work threadvoid WorkThread(){#define WORKTHREADCNT 1auto func = [](){int value = 0;int cnt = 0;boost::mutex::scoped_lock lock(_g_cv_mutex);while (!_g_is_quit){value = GetValueFromList();if (!value){_g_cv.wait(lock);continue;}printf("%3d,", value);++cnt;if (cnt != 0 && cnt % 9 == 0){printf("\n");}}};for (size_t i = 0; i < WORKTHREADCNT; i++){boost::thread th(func);}}int _tmain(int argc, _TCHAR* argv[]){_g_list = boost::shared_ptr<std::list<int>>(new std::list<int>());WorkThread();CreateThr(4);int value = 0;while (value != -1){if (value == -1){_g_is_quit = true;_g_cv.notify_all();break;}using namespace std;cout << "enter a number, enter [-1] to quit.\n";cin >> value;Add(value);}system("pause");return 0;}
0 0
- boost综合使用<function,bind,thread,mutex,condition_variable,shared_ptr>
- boost::function/bind 的使用
- 用c++11的bind,function,boost::condition_variable实现的一个线程安全的任务队列
- boost::condition_variable的使用
- boost::bind和boost::function使用示例
- boost::function与boost::bind 的使用
- c++11中的thread, mutex, condition_variable讲解
- boost thread pool the parameters of the function boost::bind()
- boost::shared_ptr Thread Safety
- boost::bind\boost::function
- boost::bind/boost::function
- boost::thread mutex
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost库 bind/function的使用
- 使用boost::bind和boost::function实现工厂模式
- C++ boost bind与 boost function的使用
- Maven使用笔记
- 简单东西-项目开发之js总结
- 【Android】【内存】Android内存相关
- 安卓第五记——通过数组改变颜色
- repeater行双击事件
- boost综合使用<function,bind,thread,mutex,condition_variable,shared_ptr>
- C++中构造函数,拷贝构造函数,析构函数
- 小赵学习Opencv(2)之椒盐噪声及图像锐化
- Mysql 拿指定经纬度与数据库大批量数据进行距离比较
- Makefile 文件学习——001
- JEEBBS在linux服务器上发布中文乱码问题
- SQLite 3.0 开发接口简介
- mysql优化笔记
- Shell入门教程:命令替换 $() 和 ``