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 ;
}

最新文章

  1. storm启动过程之源码分析
  2. Js脚本选取iframe中的元素
  3. jquery源码分析
  4. H5 -- 本地存储计数器的值 和前端校验用户
  5. fastcgi 分布式
  6. spring @bean注解
  7. 【转】android的消息处理机制(图+源码分析)——Looper,Handler,Message
  8. Hadoop源代码分析【IO专题】
  9. Android Socket编程基础
  10. TP5.0实现无限极回复功能
  11. VS2015 查看类之间的继承关系
  12. day15.Python内置函数
  13. UML 概述
  14. python3实现链表
  15. Centos7下搭建LAMP环境,安装wordpress(不会生产博客,只是一名博客搬运工)(菜鸟)
  16. H5页面调用手机打电话功能
  17. hdu 1213 求连通分量(并查集模板题)
  18. Building LinkedIn’s Real-time Activity Data Pipeline
  19. [LeetCode] Read N Characters Given Read4 I &amp; II
  20. spring boot 整合案例

热门文章

  1. TestNG(一) TestNG实战在idea中创建module
  2. C#加载前生成静态网页
  3. charles 访问控制设置
  4. 《Ansible自动化运维:技术与最佳实践》第三章读书笔记
  5. [Vue warn]: Duplicate keys detected: &#39;area&#39;. This may cause an update error.
  6. Mach-O在内存中符号表地址、字符串表地址的计算
  7. Django REST Framework序列化器
  8. Hibernate 中setResultTransformer使用
  9. css或Angular框架限制文本字数
  10. Spring Cloud 初认识