需要用到的头文件:
boost 线程使用:
创建线程组:
互斥访问:
条件变量:
实现线程间通信,信号量的传递。可以用来补充互斥变量不能完成的功能,比如说特定的条件下才满足的锁定。
只初始化一次的回调函数:
#include <iostream>#include <boost/thread/thread.hpp>#include <boost/thread/once.hpp> // Some sort of connection class that should only be initialized oncestruct Conn { static void init( ) {++i_;} static boost::once_flag init_; static int i_; // }; int Conn::i_ = 0;boost::once_flag Conn::init_ = BOOST_ONCE_INIT; void worker( ) { boost::call_once(Conn::init, Conn::init_); // Do the real work} Conn c; // You probably don't want to use a global, so see the // next Recipe int main( ) { boost::thread_group grp; for (int i = 0; i < 100; ++i) grp.create_thread(worker); grp.join_all( ); std::cout << c.i_ << '\n'; // c.i_ = 1}//使用前,必须先定义boost::once_flag,不能为临时变量//代码来自《C++ cookbook》参考文章:http://www.cppblog.com/shaker/archive/2007/10/06/33583.html
posted on 2012-02-19 22:48 帅哥帅 阅读(1424) 评论(0) 编辑 收藏 引用 所属分类: boost
Powered by: C++博客 Copyright © 帅哥帅