boost::thread编程-线程本地存储
2014-12-16 15:12
441 查看
有时候函数使用了局部静态变量或全局静态变量,因此不能用于多线程环境,因此无法保证静态变量在多线程重入时的正确操作。
boost::thread库使用thread_specific_ptr实现了可移植的线程本地存储机制(thread local storage,或者是thread specific storage,简称tss),使这样的变量用起来就像每个线程独立拥有,可以简化多线程应用,提高性能。
thread_specific_ptr的用法示例如下:
boost::thread库使用thread_specific_ptr实现了可移植的线程本地存储机制(thread local storage,或者是thread specific storage,简称tss),使这样的变量用起来就像每个线程独立拥有,可以简化多线程应用,提高性能。
thread_specific_ptr的用法示例如下:
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> #include <boost/atomic.hpp> boost::mutex io_mu;//io读写锁 void printing() { boost::thread_specific_ptr<int> pi;//线程本地存储一个整数 pi.reset(new int());//直接用reset()赋值 for(int i=0;i<5;++i) { ++ (*pi); boost::mutex::scoped_lock lock(io_mu); std::cout<<"thread id:"<<boost::this_thread::get_id()<<" print value(*pi)="<<*pi<<std::endl; } } int _tmain(int argc, _TCHAR* argv[]) { boost::thread thrd1(printing); boost::thread thrd2(printing); thrd1.join(); thrd2.join(); getchar(); return 0; }
相关文章推荐
- boost::thread编程-线程本地存储
- 一个使用boost::thread_specific_ptr线程本地存储的示例,value将不受其它线程影响
- (38)21.3.8 线程本地存储---Java编程思想之并发笔记
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
- Boost Thread 编程指南、Boost线程入门教程
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
- [并发并行]_[C/C++]_[使用线程本地存储Thread Local Storage(TLS)-win32线程和pthread线程比较]
- C#线程本地存储:LocalDataStoreSlot和ThreadLocal<T>
- [并发并行]_[C/C++]_[使用线程本地存储Thread Local Storage(TLS)调用复制文件接口的案例]
- Atitit usrqbg1821 Tls 线程本地存储(ThreadLocal Storage 规范标准化草案解决方案ThreadStatic
- Boost编程之--慎用线程的this_thread::yield()方法
- boost::thread编程-线程中断
- 线程本地存储 __declspec(thread)
- 线程本地存储(Thread Local Storage, TLS)
- 线程本地存储(Thread Local Storage, TLS)简单分析与使用
- boost::thread编程-线程创建
- 现代c++之线程本地存储thread_local
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理