boost 学习之-Synchronization

本文概要介绍了Boost库中的同步与并发组件,包括互斥锁、条件变量、屏障、未来等概念,以及如何解决仅初始化一次问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要点:

1 boost提供了如下lock模板类,以mutex类型作为模板参数

Class template lock_guardClass template unique_lockClass template shared_lockClass template upgrade_lockClass template upgrade_to_unique_lockMutex-specific class scoped_try_lock

2 boost提供了如下mutex类型,分别用于上面的lock模板类的参数

Class mutexTypedef try_mutexClass timed_mutexClass recursive_mutexTypedef recursive_try_mutexClass recursive_timed_mutexClass shared_mutex
3 condition 类型

Class condition_variableClass condition_variable_anyTypedef 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文档;该内容很实用;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值