自己学习 boost 线程库时写的小代码
2008-10-14 00:03
281 查看
1.简介
thread_specific_ptr class 定义了相关于线程的存储接口。很拗口,其实就是对TLS
Thread-Locally Storage的包装。它可用于封装线程独立的全局变量。
thread_specific_ptr 对象为每个线程保持一个指针,每个线程都应该new出一个对
象交给thread_specific_ptr,当线程终结时,该对象释放。当线程初始化时,线程中
的thread_specific_ptr实例保存的是null指针。
template thread_specific_ptr对以下情况很有用:
改编一个原本设计用于单线程的库接口,比如libc里的strtok函数。这种库一般隐含
的使用一个全局变量,可以使用thread_specific_ptr控制全局变量,使其可用于多
线程。
线程中使用了一系列的方法/函数,它们需要一个逻辑上的全局变量来共享数据,但
实际上这个变量是线程独立的。
2.使用
它的接口和scope_ptr基本一样,这里就不多说了。
3.例子
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>
#include <cassert>
#include <iostream>
using namespace std;
// 这里定义了一个线程独立的全局变量
boost::thread_specific_ptr<int> value;
void increment()
{
int* p = value.get();
++*p;
}
void thread_proc()
{
value.reset(new int(0)); // 每个线程初始化自己的value。
for (int i=0; i<10; ++i)
{
increment();
int* p = value.get();
assert(*p == i+1); // 这里检查是不是有别的thread修改本线程的value
}
}
int main(int argc, char* argv[])
{
boost::thread_group threads;
for (int i=0; i<5; ++i)
threads.create_thread(&thread_proc);
threads.join_all();
system("pause");
return 0;
}
// 照虎画猫; 使用 boost::thread_specific_ptr 调用类对象方法
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/tss.hpp>
#include <iostream>
class CTest;
boost::mutex io_mutex;
boost::thread_specific_ptr< CTest > ptrTest;
using namespace std;
class CTest
{
public:
CTest(int id)
{
m_id = id;
m_value = 0;
}
void CountValue()
{
while ( m_value++ < 10 )
{
boost::mutex::scoped_lock
lock(io_mutex);
cout << m_id << ": " << m_value << endl;
}
}
private:
int m_value;
int m_id;
};
void Test1()
{
if (ptrTest.get() == NULL)
ptrTest.reset( new CTest(1) );
ptrTest->CountValue();
}
void Test2()
{
if (ptrTest.get() == NULL)
ptrTest.reset( new CTest(2) );
ptrTest->CountValue();
}
int main(int argc, char* argv[])
{
boost::thread thrd1( Test1 );
boost::thread thrd2( Test2 );
thrd1.join();
thrd2.join();
std::system("pause");
return 0;
}
thread_specific_ptr class 定义了相关于线程的存储接口。很拗口,其实就是对TLS
Thread-Locally Storage的包装。它可用于封装线程独立的全局变量。
thread_specific_ptr 对象为每个线程保持一个指针,每个线程都应该new出一个对
象交给thread_specific_ptr,当线程终结时,该对象释放。当线程初始化时,线程中
的thread_specific_ptr实例保存的是null指针。
template thread_specific_ptr对以下情况很有用:
改编一个原本设计用于单线程的库接口,比如libc里的strtok函数。这种库一般隐含
的使用一个全局变量,可以使用thread_specific_ptr控制全局变量,使其可用于多
线程。
线程中使用了一系列的方法/函数,它们需要一个逻辑上的全局变量来共享数据,但
实际上这个变量是线程独立的。
2.使用
它的接口和scope_ptr基本一样,这里就不多说了。
3.例子
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>
#include <cassert>
#include <iostream>
using namespace std;
// 这里定义了一个线程独立的全局变量
boost::thread_specific_ptr<int> value;
void increment()
{
int* p = value.get();
++*p;
}
void thread_proc()
{
value.reset(new int(0)); // 每个线程初始化自己的value。
for (int i=0; i<10; ++i)
{
increment();
int* p = value.get();
assert(*p == i+1); // 这里检查是不是有别的thread修改本线程的value
}
}
int main(int argc, char* argv[])
{
boost::thread_group threads;
for (int i=0; i<5; ++i)
threads.create_thread(&thread_proc);
threads.join_all();
system("pause");
return 0;
}
// 照虎画猫; 使用 boost::thread_specific_ptr 调用类对象方法
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/tss.hpp>
#include <iostream>
class CTest;
boost::mutex io_mutex;
boost::thread_specific_ptr< CTest > ptrTest;
using namespace std;
class CTest
{
public:
CTest(int id)
{
m_id = id;
m_value = 0;
}
void CountValue()
{
while ( m_value++ < 10 )
{
boost::mutex::scoped_lock
lock(io_mutex);
cout << m_id << ": " << m_value << endl;
}
}
private:
int m_value;
int m_id;
};
void Test1()
{
if (ptrTest.get() == NULL)
ptrTest.reset( new CTest(1) );
ptrTest->CountValue();
}
void Test2()
{
if (ptrTest.get() == NULL)
ptrTest.reset( new CTest(2) );
ptrTest->CountValue();
}
int main(int argc, char* argv[])
{
boost::thread thrd1( Test1 );
boost::thread thrd2( Test2 );
thrd1.join();
thrd2.join();
std::system("pause");
return 0;
}
相关文章推荐
- 《APUE》Chapter 10 Signals (学习笔记加上自己的代码)
- 《APUE》chapter 12 Thread control 学习笔记(加上自己的代码)
- git学习--repo批量创建远端仓库----建立自己的android代码仓库
- python学习第二章变量和简单数据类型的部分课后练习自己尝试的代码
- 由成幻OnlineJudge学习如何做自己的Acm-Icpc在线评判系统-5.在线编译与测试系统代码粗解
- 《APUE》chapter 5 Standard I/O libary 学习笔记(加上自己的代码)
- 自己编写的泛型List<T>代码,供大家学习,欢迎指导
- 一些自己学习的html代码(锚,动态改变文本和链接,有序无序列表等)
- 学习《Real Time Rendering》后自己实现的代码
- PHP学习笔记,自己动手写个MVC的框架 -- base所有代码
- [学习笔记]Java代码构建一个线程池的自己学习写的实例,用这个你会更好的理解文章内容
- 向身边的巨人学习如何写好代码,刻意去提升自己
- 关于自己的游戏《小鱼吃苹果》 J2ME和android代码提供给大家学习
- [ay原创作品]用wpf写了个模仿36Kr网站登录背景的效果-【鄙视博客园,已被移出首页,代码在我自己官网上,自己下载学习吧】
- U3D中在Grid中增加和修改Cell的部分代码 - 自己学习积累
- cocos2d-xna-TweeJump学习笔记1(都是自己看别人代码后所感所想,希望有懂的人指出我的错误或者大家交流交流)
- AJAX学习 自己动手写ajax登录验证及首页数据绑定(代码片段,主要是登录js,ajax的get和post请求)
- ASP.NET学习笔记2--自己写代码绑定Gridview
- 如何自己编译apue.3e中代码 & 学习写makefile
- ASP.NET学习笔记2--自己写代码绑定Gridview