要点:
1 boost提供了如下lock模板类,以mutex类型作为模板参数
- Class template
lock_guard
Class template unique_lock
Class template shared_lock
Class template upgrade_lock
Class template upgrade_to_unique_lock
Mutex-specific class scoped_try_lock
2 boost提供了如下mutex类型,分别用于上面的lock模板类的参数
- Class
mutex
Typedef try_mutex
Class timed_mutex
Class recursive_mutex
Typedef recursive_try_mutex
Class recursive_timed_mutex
Class shared_mutex
- Class
condition_variable
Class condition_variable_any
Typedef condition
4 仅初始化一次问题
#include <boost/thread/once.hpp>
boost::once_flag f=BOOST_ONCE_INIT;
template<typename Callable> void call_once(once_flag& flag,Callable func);
void call_once(void (*func)(),once_flag& flag);两个函数,一个flag,用于程序中仅调用一次func
5 Barriers
class barrier { public: barrier(unsigned int count); ~barrier(); bool wait(); };
障碍是一个同步点,在wait的地方,必须count个线程都执行到该位置,才开始wait后面的内容;
6 futures
应用场合是:一个线程发出命令,另外一个线程执行命令,发出命令线程通过future获得命令的执行结果;详细内容参考分布式网络编程内容《面向模式的软件体系结构卷2》
相关的类有boost::promise
or a
boost::packaged_task
;
总结,同步内容很多,详细参考boost文档;该内容很实用;