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

 boost::interprocess::interprocess_semaphore m_semaphore();

然后就是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,一开始不知道,试了几次不行,后面灵机一动看了下源码,原来如此.

 inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
{
if(abs_time == boost::posix_time::pos_infin){
this->wait();
return true;
}
//Obtain current count and target time
boost::posix_time::ptime now(microsec_clock::universal_time());
if(now >= abs_time)
return false; do{
if(this->try_wait()){
break;
}
now = microsec_clock::universal_time(); if(now >= abs_time){
return this->try_wait();
}
// relinquish current time slice
detail::thread_yield();
}while (true);
return true;
}

下面举个栗子

 #include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
boost::interprocess::interprocess_semaphore m_semaphore();
void test(int i)
{
while(true)
{
if(m_semaphore.try_wait())
{
printf("%d\n",i);
m_semaphore.post(); }
boost::this_thread::sleep(boost::posix_time::seconds()); }
}
using namespace boost::posix_time;
int main()
{
boost::thread thread1(boost::bind(&test,));
boost::thread thread2(boost::bind(&test,));
getchar(); }

boost信号量 boost::interprocess::interprocess_semaphore的用法的更多相关文章

  1. 【Boost】boost库中timer定时器 2

    博客转载自:http://blog.csdn.net/yockie/article/details/40386145 先跟着boost文档中asio章节的指南中的几个例子学习一下使用: 所有的Asio ...

  2. boost::thread boost库线程

    一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...

  3. 【Boost】boost库asio详解3——io_service作为work pool

    无论如何使用,都能感觉到使用boost.asio实现服务器,不仅是一件非常轻松的事,而且代码很漂亮,逻辑也相当清晰,这点上很不同于ACE.使用io_service作为处理工作的work pool,可以 ...

  4. 初识boost之boost::share_ptr用法

    boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...

  5. 【Boost】boost库asio详解5——resolver与endpoint使用说明

    tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...

  6. [boost] build boost with intel compiler 16.0.XXX

    Introduction There are few information about how to compile boost with Intel compiler. This article ...

  7. Boost 和 Boost.Build 的设置

    问题: 安装编译完 Boost 后,如果不设置 BOOST_ROOT 和 BOOST_BUILD_PATH 则可能导致使用 bjam 时定位到 Boost 默认的路径 /usr/share/boost ...

  8. 【Boost】boost::tokenizer详解

    分类: [C++]--[Boost]2012-12-28 21:42 2343人阅读 评论(0) 收藏 举报   目录(?)[+]   tokenizer 库提供预定义好的四个分词对象, 其中char ...

  9. 【Boost】boost::string_algo详解2——find相关函数

    来自: https://blog.csdn.net/huang_xw/article/details/8276123 函数声明:   template<typename Range1T, typ ...

随机推荐

  1. WinForm常用代码

    //ToolStripSplitButton是标准按钮和下拉按钮的组合,各自工作,但有联系,感觉上后者是没有向下箭头ToolStripDropDownButton:ToolStripDropDownB ...

  2. C#中decimal的用法

    decimal拥有比float更高的精度,最高能处理到小数点后面的28位.适合用在财务类等对数字精确度要求比较高的场合. using System; using System.Collections. ...

  3. JDBC_mysql---防sql注入,存储图片

    package PreparedStatement_sql注入; import java.io.File; import java.io.FileInputStream; import java.io ...

  4. Spring--------web应用中保存spring容器

    ---恢复内容开始--- 问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托 ...

  5. SDK编程模板

    #include<Windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINS ...

  6. 再谈Android应用瘦身

    Android应用apk安装包的大小,虽然对于现在WiFi普及情况而言消耗流量已经微乎其微,但是,对于一款好的应用,对于一款负责任的应用,当然是越小越好了. 引言: .应用越小,下载越快,也就意味着新 ...

  7. Android url中文编码问题

    最近项目遇见一个很奇葩问题,关于URL问题,项目中加载图片,图片的URL含有中文,但是,我的手机可以加载,没问题,同事也都可以,但是测试手机却不可以,加载失败,找到问题,就是URL含有中文问题. 解决 ...

  8. ZRender源码分析2:Storage(Model层)

    回顾 上一篇请移步:zrender源码分析1:总体结构 本篇进行ZRender的MVC结构中的M进行分析 总体理解 上篇说到,Storage负责MVC层中的Model,也就是模型,对于zrender来 ...

  9. A Simple Task

    A Simple Task Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. #pragma pack(n) 的作用

    在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float等)的变量,也可以是一些复合数据类型(如数组.结构.联合等)的数据单元.在结构中,编译器为结构的每个成 ...