Chinaunix首页 | 论坛 | 博客
  • 博客访问: 923968
  • 博文数量: 335
  • 博客积分: 10287
  • 博客等级: 上将
  • 技术积分: 3300
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 15:29
文章分类

全部博文(335)

文章存档

2015年(4)

2014年(15)

2013年(17)

2012年(11)

2011年(12)

2010年(96)

2009年(27)

2008年(34)

2007年(43)

2006年(39)

2005年(37)

我的朋友

分类: C/C++

2010-03-10 13:00:21

多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage。
boost::thread库为我们提供了一个接口简单的TLS的面向对象的封装,以下是tss类的接口定义:

  1. class tss  
  2. {  
  3. public:  
  4.     tss(boost::function1<voidvoid*>* pcleanup);  
  5.     void* get() const;  
  6.     void set(void* value);  
  7.     void cleanup(void* p);  
  8. };  

分别用于获取、设置、清除线程局部存储变量,这些函数在内部封装了TlsAlloc、TlsGetValue、TlsSetValue等API操作,将它们封装成了OO的形式。
但boost将该类信息封装在detail名字空间内,即不推荐我们使用,当需要使用tss时,我们应该使用另一个使用更加方便的类:thread_specific_ptr,这是一个智能指针类,该类的接口如下:

  1. class thread_specific_ptr : private boost::noncopyable   // Exposition only  
  2. {  
  3. public:  
  4.   // construct/copy/destruct  
  5.   thread_specific_ptr();  
  6.   thread_specific_ptr(void (*cleanup)(void*));  
  7.   ~thread_specific_ptr();  
  8.  
  9.   // modifier functions  
  10.   T* release();  
  11.   void reset(T* = 0);  
  12.  
  13.   // observer functions  
  14.   T* get() const;  
  15.   T* operator->() const;  
  16.   T& operator*()() const;  
  17. };  
  18.  

即可支持get、reset、release等操作。
thread_specific_ptr类的实现十分简单,仅仅为了将tss类“改装”成智能指针的样子,该类在其构造函数中会自动创建一个tss对象,而在其析构函数中会调用默认参数的reset函数,从而引起内部被封装的tss对象被析构,达到“自动”管理内存分配释放的目的。

以下是一个运用thread_specific_ptr实现TSS的例子:
 

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.  
  6. boost::mutex io_mutex;  
  7. boost::thread_specific_ptr<int> ptr;  
  8. // use this method to tell that this member will not shared by all threads  
  9.  
  10. struct count  
  11. {  
  12.     count(int id) : id(id) { }  
  13.  
  14.     void operator()()  
  15.     {  
  16.         if (ptr.get() == 0)    // if ptr is not initialized, initialize it  
  17.             ptr.reset(new int(0));
  18. // Attention, we pass a pointer to reset (actually set ptr)  
  19.  
  20.         for (int i = 0; i < 10; ++i)  
  21.         {  
  22.             (*ptr)++;  
  23.             boost::mutex::scoped_lock lock(io_mutex);  
  24.             std::cout << id << ": " << *ptr << std::endl;  
  25.         }  
  26.     }  
  27.  
  28.     int id;  
  29. };  
  30.  
  31. int main(int argc, char* argv[])  
  32. {  
  33.     boost::thread thrd1(count(1));  
  34.     boost::thread thrd2(count(2));  
  35.     thrd1.join();  
  36.     thrd2.join();  
  37.  
  38.     return 0;  
  39. }  

此外,thread库还提供了一个很有趣的函数,call_once,在tss::init的实现中就用到了该函数。
该函数的声明如下:
void call_once(void (*func)(), once_flag& flag);
该函数的Windows实现通过创建一个Mutex使所有的线程在尝试执行该函数时处于等待状态,直到有一个线程执行完了func函数,该函数的第二个参数表示函数func是否已被执行,该参数往往被初始化成BOOST_ONCE_INIT(即0),如果你将该参数初始化成1,则函数func将不被调用,此时call_once相当于什么也没干,这在有时候可能是需要的,比如,根据程序处理的结果决定是否需要call_once某函数func。
call_once在执行完函数func后,会将flag修改为1,这样会导致以后执行call_once的线程(包括等待在Mutex处的线程和刚刚进入call_once的线程)都会跳过执行func的代码。

需要注意的是,该函数不是一个模板函数,而是一个普通函数,它的第一个参数1是一个函数指针,其类型为void (*)(),而不是跟boost库的很多其它地方一样用的是function模板,不过这样也没有关系,有了boost::bind这个超级武器,想怎么绑定参数就随你的便了,根据boost的文档,要求传入的函数不能抛出异常,但从实现代码中好像不是这样。

以下是一个典型的运用call_once实现一次初始化的例子:

  1. #include   
  2. #include   
  3. #include   
  4.  
  5. int i = 0;  
  6. int j = 0;  
  7. boost::once_flag flag = BOOST_ONCE_INIT;  
  8.  
  9. void init()  
  10. {  
  11.     ++i;  
  12. }  
  13.  
  14. void thread()  
  15. {  
  16.     boost::call_once(&init, flag);  
  17.     ++j;  
  18. }  
  19.  
  20. int main(int argc, char* argv[])  
  21. {  
  22.     boost::thread thrd1(&thread);  
  23.     boost::thread thrd2(&thread);  
  24.     thrd1.join();  
  25.     thrd2.join();  
  26.  
  27.     std::cout << i << std::endl;  
  28.     std::cout << j << std::endl;  
  29.  
  30.     return 0;  
  31. }  
  32.  

结果显示,全局变量i仅被执行了一次++操作,而变量j则在两个线程中均执行了++操作。

其它
boost::thread目前还不十分完善,最主要的问题包括:没有线程优先级支持,或支持线程的取消操作等,而且,目前的实现机制似乎不大容易通过简单修改达到这一要求,也许将来的某个版本会在实现上出现较大调整,但目前支持的接口应该会相对保持稳定,目前支持的特性也还会继续有效。

原文:http://blog.vckbase.com/billdavid/archive/2005/05/24/5736.html

阅读(1128) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~