boost信号量 boost::interprocess::interprocess_semaphore的用法
使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数。
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的用法的更多相关文章
- 【Boost】boost库中timer定时器 2
博客转载自:http://blog.csdn.net/yockie/article/details/40386145 先跟着boost文档中asio章节的指南中的几个例子学习一下使用: 所有的Asio ...
- boost::thread boost库线程
一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...
- 【Boost】boost库asio详解3——io_service作为work pool
无论如何使用,都能感觉到使用boost.asio实现服务器,不仅是一件非常轻松的事,而且代码很漂亮,逻辑也相当清晰,这点上很不同于ACE.使用io_service作为处理工作的work pool,可以 ...
- 初识boost之boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- 【Boost】boost库asio详解5——resolver与endpoint使用说明
tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...
- [boost] build boost with intel compiler 16.0.XXX
Introduction There are few information about how to compile boost with Intel compiler. This article ...
- Boost 和 Boost.Build 的设置
问题: 安装编译完 Boost 后,如果不设置 BOOST_ROOT 和 BOOST_BUILD_PATH 则可能导致使用 bjam 时定位到 Boost 默认的路径 /usr/share/boost ...
- 【Boost】boost::tokenizer详解
分类: [C++]--[Boost]2012-12-28 21:42 2343人阅读 评论(0) 收藏 举报 目录(?)[+] tokenizer 库提供预定义好的四个分词对象, 其中char ...
- 【Boost】boost::string_algo详解2——find相关函数
来自: https://blog.csdn.net/huang_xw/article/details/8276123 函数声明: template<typename Range1T, typ ...
随机推荐
- C#中WinForm程序退出方法技巧总结
C#中WinForm程序退出方法技巧总结 一.关闭窗体 在c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.Ex ...
- node.js入门(二) 第一个程序 Hello World
新建一个名为"hello.js"文本文件,然后输入如下内容 //载入http模块 var http = require('http'); //构建一个http服务器 var ser ...
- Go 语言读写 Excel
Excelize 是 Golang 编写的一个用来操作 Office Excel 文档类库,基于微软的 Office OpenXML 标准.可以使用它来读取.写入 XLSX 文件.相比较其他的开源类库 ...
- TextArea里Placeholder换行问题
页面上使用TextArea控件时,会时不时的想给个提示,比如按照一定方式操作之类的.正常情况下,会使用Placeholder,但这样的提示是不会换行的,无论是用\r\n,还是用<br/>, ...
- laravel artisan 命令工具
//全局相关 php artisan:显示详细的命令行帮助信息,同 php artisan list php artisan –help:显示帮助命令的使用格式,同 php artisan help ...
- (2012年旧文)纪念史蒂夫乔布斯---IT界的普罗米修斯
谈苹果与乔布斯系列一 IT界的普罗米修斯 纪念PC界的先驱 史蒂夫乔布斯 2012-4-5 清明节,纪念IT时代的开创人—伟大的史蒂夫 乔布斯. 没有乔布斯,计算机还是属于一群科技人士的工具,没有漂 ...
- HD1083 二分图,匈牙利算法
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdio.h> #include<stdlib. ...
- linux case 语句
#!/bin/bash #$ 表示脚本名 #$n 表示第n个参数(n>) in ") echo '--=> A' ;; ") echo '--=> B' ;; * ...
- 解决C/C++程序执行一闪而过的方法(三种办法)
简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...
- Ubuntu 安装 Eclipse C/C++开发环境
所需软件清单: 1.eclipse-linuxtools-indigo-SR1-incubation-linux-gtk.tar.gz 2.jre-7u2-linux-i586.tar.gz 先将上述 ...