使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数。

1 boost::interprocess::interprocess_semaphore m_semaphore(0);

然后就是pv操作了,v操作就只有一个post(),post()一次,信号量加1.p操作有三个,看函数名字都很明显知道是什么意思,

wait(),try_wait() ,timed_wait(const boost::posix_time::ptime&abs_time).

这里需要注意的是第三个p操做,对boost也不太熟, 很明显是等待到某一时间点,m_semaphore.timed_wait(boost::posix_time::ptime(second_clock::universal_time()+boost::posix_time::seconds(1))),等待一秒;这个时间是取universal_time,一开始不知道,试了几次不行,后面灵机一动看了下源码,原来如此.

 1 inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
 2 {
 3    if(abs_time == boost::posix_time::pos_infin){
 4       this->wait();
 5       return true;
 6    }
 7    //Obtain current count and target time
 8    boost::posix_time::ptime now(microsec_clock::universal_time());
 9    if(now >= abs_time)
10       return false;
11
12    do{
13       if(this->try_wait()){
14          break;
15       }
16       now = microsec_clock::universal_time();
17
18       if(now >= abs_time){
19          return this->try_wait();
20       }
21       // relinquish current time slice
22       detail::thread_yield();
23    }while (true);
24    return true;
25 }

下面举个栗子

 1 #include <boost/interprocess/sync/interprocess_semaphore.hpp>
 2 #include <boost/thread.hpp>
 3 #include <boost/bind.hpp>
 4 #include <boost/date_time/posix_time/posix_time.hpp>
 5 #include <iostream>
 6 using namespace std;
 7 boost::interprocess::interprocess_semaphore m_semaphore(1);
 8 void test(int i)
 9 {
10     while(true)
11     {
12         if(m_semaphore.try_wait())
13         {
14             printf("%d\n",i);
15             m_semaphore.post();
16
17         }
18         boost::this_thread::sleep(boost::posix_time::seconds(2));
19
20     }
21 }
22 using namespace boost::posix_time;
23 int main()
24 {
25     boost::thread thread1(boost::bind(&test,1));
26     boost::thread thread2(boost::bind(&test,2));
27     getchar();
28
29 }

转载于:https://www.cnblogs.com/doutu/p/4848555.html

boost信号量 boost::interprocess::interprocess_semaphore的用法相关推荐

  1. boost::interprocess::ipcdetail::private_adaptive_node_pool用法的测试程序

    boost::interprocess::ipcdetail::private_adaptive_node_pool用法的测试程序 实现功能 C++实现代码 实现功能 boost::interproc ...

  2. boost::metaparse::v1::impl::empty_string相关用法的测试程序

    boost::metaparse::v1::impl::empty_string相关用法的测试程序 实现功能 C++实现代码 实现功能 boost::metaparse::v1::impl::empt ...

  3. boost::math::boost::math::interpolators::cardinal_cubic_b_spline用法的测试程序

    boost::math::boost::math::interpolators::cardinal_cubic_b_spline用法的测试程序 实现功能 C++实现代码 实现功能 boost::mat ...

  4. boost::geometry::strategy::distance::detail::projected_point_ax用法的测试程序

    boost::geometry::strategy::distance::detail::projected_point_ax用法的测试程序 实现功能 C++实现代码 实现功能 boost::geom ...

  5. boost::format模块format的基本用法示例

    boost::format模块format的基本用法示例 实现功能 C++实现代码 实现功能 boost::format模块format的基本用法示例 C++实现代码 #include "b ...

  6. boost::format模块format的高级用法示例

    boost::format模块format的高级用法示例 实现功能 C++实现代码 实现功能 boost::format模块format的高级用法示例 C++实现代码 #include <ios ...

  7. boost::container模块实现范围分配器用法

    boost::container模块实现范围分配器用法 实现功能 C++实现代码 实现功能 boost::container模块实现范围分配器用法 C++实现代码 #include <memor ...

  8. Boost:boost::source_location的用法测试程序

    Boost:boost::source_location的用法测试程序 实现功能 C++实现代码 实现功能 boost::source_location的用法测试程序 C++实现代码 #include ...

  9. Boost:Boost概念的function_requires()的测试程序

    Boost:Boost概念的function_requires()的测试程序 实现功能 C++实现代码 实现功能 Boost:Boost概念的function_requires()的测试程序 C++实 ...

最新文章

  1. python简单编程语言_功能强大而又简单易学的编程语言Python
  2. Swift版iOS游戏框架Sprite Kit基础教程下册
  3. 《Android的设计与实现:卷I》——第2章 框架基础JNI
  4. 看看人家那后端API接口写得,那叫一个巴适~,再看看我的,像坨屎!
  5. Oracle12如何重装,Oracle怎么重新安装?
  6. mysql 引擎 校对,MySQL 字符集和校对
  7. react学习(22)---需要export
  8. html动态跟随鼠标效果,使用JS实现气泡跟随鼠标移动的动画效果
  9. 《学习之道》第十三章练习大脑,改变思维
  10. 第一次当项目经理压力大_项目经理不想被甩锅,你要这样做进度管理
  11. .net 把一个对象赋值给一个参数_一个可以提升JVM运行性能的参数
  12. 【ML课 刘学军】 第一课 20200907
  13. 百战程序员python900集资源_尚学堂百战程序员:盘点那些实用的Python代码
  14. vs.Net 2003 安装
  15. 阿里云企业邮箱没有活动怎么办?实测:代金券也可节约成本
  16. 2023四川大学计算机考研信息汇总
  17. KNN算法优缺点总结,以及机器学习流程的总结
  18. 最优传输论文(十七):Sinkhorn Distances: Lightspeed Computation of Optimal Transport论文原理
  19. 电脑使用技巧 (C盘空间扩容)
  20. 企业管理软件从勤哲excel服务器迁移到奥多odoo的感触

热门文章

  1. 成功从小公司跳槽!35岁的程序员被裁
  2. 【Linux入门到精通系列讲解】Nginx详细介绍和安装使用(这一篇就够了)
  3. aix oracle 登录用户,AIX 系统及 Oracle 数据库用户权限管理
  4. mysql, 一对多查询, 统计一表数量
  5. python commands模块_python commands模块在python3.x被subprocess取代
  6. 中山网络推广解答网站排名优化为什么总是上不了首页!
  7. 网络推广外包浅析提升移动端网站建设效率有哪些网络推广外包技巧
  8. 网络推广专员浅析网络推广期间网站收录如何提升?
  9. java opencv 平移_如何使用opencv pnpRansac()函数中的平移矩阵和旋转矩阵设置Rajawali相机的旋转?...
  10. php文件在网页,php文件读取到网页中