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