天天看點

Boost Thread學習筆記五

多線程程式設計中還有一個重要的概念: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

 9 struct count

10 {

11     count(int id) : id(id) { }

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實作一次初始化的例子:

 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;

 9 void init()

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則在兩個線程中均執行了++操作。

繼續閱讀