多線程編程中還有一個重要的概念:Thread Local Store(TLS,線程局部存儲),在boost中,TLS也被稱作TSS,Thread Specific Storage。
boost
::thread庫為我們提供了一個接口簡單的TLS的面向對象的封裝,以下是tss類的接口定義:
class tss
{
public:
tss(boost::function1<void, void*>* pcleanup);
void* get() const;
void set(void* value);
void cleanup(void* p);
};
分別用于獲取、設置、清除線程局部存儲變量,這些函數在內部封裝了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 };
即可支持get、reset、release等操作。
thread_specific_ptr類的實現十分簡單,僅僅為了將tss類“改裝”成智
能指針的樣子,該類在其構造函數中會自動創建一個tss對象,而在其析構函數中會調用默認參數的reset函數,從而引起內部被封裝的tss對象被析構,
達到“自動”管理內存分配釋放的目的。
以下是一個運用thread_specific_ptr實現TSS的例子:
1 #include <boost/thread/thread.hpp>
2 #include <boost/thread/mutex.hpp>
3 #include <boost/thread/tss.hpp>
4 #include <iostream>
5
6 boost::mutex io_mutex;
7 boost::thread_specific_ptr<int> ptr; // use this method to tell that this member will not shared by all threads
8
9 struct count
10 {
11 count(int id) : id(id) { }
12
13 void operator()()
14 {
15 if (ptr.get() == 0) // if ptr is not initialized, initialize it
16 ptr.reset(new int(0)); // Attention, we pass a pointer to reset (actually set ptr)
17
18 for (int i = 0; i < 10; ++i)
19 {
20 (*ptr)++;
21 boost::mutex::scoped_lock lock(io_mutex);
22 std::cout << id << ": " << *ptr << std::endl;
23 }
24 }
25
26 int id;
27 };
28
29 int main(int argc, char* argv[])
30 {
31 boost::thread thrd1(count(1));
32 boost::thread thrd2(count(2));
33 thrd1.join();
34 thrd2.join();
35
36 return 0;
37 }
此外,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 <boost/thread/thread.hpp>
2 #include <boost/thread/once.hpp>
3 #include <iostream>
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 }
結果顯示,全局變量i僅被執行了一次
++操作,而變量j則在兩個線程中均執行了
++操作。