boost::thread_specific_ptr
thread_specific_ptr代表了一个全局的变量,而在每个线程中都各自new一个线程本地的对象交给它进行管理。
线程之间就不会因为访问同一全局对象而引起资源竞争导致性能下降。
而线程结束时,这个资源会被自动释放。
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/tss.hpp>
#include <iostream>
boost::mutex io_mutex;
boost::thread_specific_ptr<int> ptr;
struct stcount
{
stcount(int id) : id(id) { }
void operator()()
{
if (ptr.get() == )
ptr.reset(new int());
for (int i = ; i < ; ++i)
{
(*ptr)++;
boost::mutex::scoped_lock
lock(io_mutex);
std::cout << id << ": " << *ptr << std::endl;
}
}
int id;
}; int _tmain(int argc, _TCHAR* argv[])
{
boost::thread thrd1(stcount());
boost::thread thrd2(stcount());
thrd1.join();
thrd2.join();
while ();
return ;
}
boost::thread_specific_ptr的更多相关文章
- boost muti-thread
背景 • 今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率:为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序: GUI应用程序将那些费时,复杂的处理以线程的形式单独 ...
- BOOST 线程完全攻略 - 基础篇
http://blog.csdn.net/iamnieo/article/details/2908621 2008-09-10 12:48 9202人阅读 评论(3) 收藏 举报 thread多线程l ...
- (十二)boost库之多线程高级特性
(十二)boost库之多线程高级特性 很多时候,线程不仅仅是执行一些耗时操作,可能我们还需要得到线程的返回值,一般的处理方法就是定义一个全局状态变量,不断轮训状态,就如我目前维护的一个项目,全局变量定 ...
- boost::thread类
前言 标准C++线程即将到来.预言它将衍生自Boost线程库,现在让我们探索一下Boost线程库. 几年前,用多线程执行程序还是一件非比寻常的事.然而今天互联网应用服务程序普遍使用多线程来提高与多客户 ...
- Boost Thread学习笔记五
多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thr ...
- boost多线程入门介绍
:first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...
- Boost多线程编程
Boost多线程编程 背景 • 今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率:为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序: GUI应用程序将那些费时, ...
- BOOST 线程完全攻略
1 创建线程 首先看看boost::thread的构造函数吧,boost::thread有两个构造函数: (1)thread():构造一个表示当前执行线程的线程对象: (2)explicit thre ...
- BOOST学习笔记
BOOST学习笔记 1 tool #pragma once #include <vector> #include "boost/noncopyable.hpp" #in ...
随机推荐
- C语言入门-循环
一.循环 输入一个数字,输出该数字有几位 #include <stdio.h> int main() { int x; int n = 0; scanf("%d" , ...
- 一个简单的jquery ajax表单提交 带数据校验 layer弹框提示
<input type="hidden" id="url" value="index.php"/> <form id=&q ...
- 如何快速转载CSDN中的博客
看到一篇<如何快速转载CSDN中的博客>,介绍通过检查元素→复制html来实现快速转载博客的方法.不过,不知道是我没有领会其精神还是其他原因,测试结果为失败.
- Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)
Spring Boot auto-configuration attempts to automatically configure your Spring application based on ...
- 单例模式-全局可用的 context 对象,这一篇就够了
单例模式在各个方面都有着极为广泛的使用,所谓单例,顾名思义就是整个程序中只有一个该类的实例,所以它成功保证了整个程序的生命周期内该类的对象只能创建一次,并且提供全局唯一访问该类的方法:getInsta ...
- Linux 文件复制命令cp
文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... ...
- Linux系统学习之Ln(软连接和硬链接)
可简单理解为,软连接:创建的软连接文件是源文件的快捷方式,删除创建的软连接文件,源文件不受影响,连接消失. 硬链接:两个连体的文件,修改其中一个文件,另外一个文件也会随之更改:删除其中一个文件,另外一 ...
- phaser学习总结之Text对象详解
前言 在phaser学习总结之phaser入门教程中,我们已经入门了phaser,对phaser也有所了解但是我们并没有对phaser中的每个对象的属性和方法进行详解,本章将对phaser中的Text ...
- ELK 学习笔记之 Logstash之filter配置
Logstash之filter: json filter: input{ stdin{ } } filter{ json{ source => "message" } } o ...
- IDEA 学习笔记之 Scala项目开发
Scala项目开发: 由于直接下载Scala plugin太慢,老是中断,所以手动下载: https://plugins.jetbrains.com/ 手动安装Scala plugin: 新建Scal ...