boost::thread_specific_ptr
2024-08-28 01:48:00
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 ;
}
最新文章
- storm启动过程之源码分析
- Js脚本选取iframe中的元素
- jquery源码分析
- H5 -- 本地存储计数器的值 和前端校验用户
- fastcgi 分布式
- spring @bean注解
- 【转】android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Hadoop源代码分析【IO专题】
- Android Socket编程基础
- TP5.0实现无限极回复功能
- VS2015 查看类之间的继承关系
- day15.Python内置函数
- UML 概述
- python3实现链表
- Centos7下搭建LAMP环境,安装wordpress(不会生产博客,只是一名博客搬运工)(菜鸟)
- H5页面调用手机打电话功能
- hdu 1213 求连通分量(并查集模板题)
- Building LinkedIn’s Real-time Activity Data Pipeline
- [LeetCode] Read N Characters Given Read4 I &; II
- spring boot 整合案例
热门文章
- TestNG(一) TestNG实战在idea中创建module
- C#加载前生成静态网页
- charles 访问控制设置
- 《Ansible自动化运维:技术与最佳实践》第三章读书笔记
- [Vue warn]: Duplicate keys detected: &#39;area&#39;. This may cause an update error.
- Mach-O在内存中符号表地址、字符串表地址的计算
- Django REST Framework序列化器
- Hibernate 中setResultTransformer使用
- css或Angular框架限制文本字数
- Spring Cloud 初认识