Boost:: thread brief analysis (3): thread local storage, and other

  Multi-threaded programming in another important concept: Thread Local Store (TLS, thread local storage), in the boost, the TLS also known as TSS, Thread Specific Storage. 
  Boost:: thread library provided us with a simple interface to the object-oriented TLS package, the following is tss of the interface definition: 
  Class tss 
  ( 
  Public: 
  Tss (boost:: function1 <void, void *> * pcleanup); 
  Void * get () const; 
  Void set (void * value); 
  Void cleanup (void * p); 
  ); 
  Were used to acquire, set up, removal of thread local storage variables, these functions in-house package, TlsAlloc, TlsGetValue, such as API TlsSetValue operation, OO them in the form of a package. 
  However, the type of information package boost in detail within the name space, we do not recommend the use, when to use tss, we should use another use more convenient categories: thread_specific_ptr, this is a smart pointer types, such access I as follows: 
  Class thread_specific_ptr: private boost:: noncopyable / / Exposition only 
  ( 
  Public: 
  / / Construct / copy / destruct 
  Thread_specific_ptr (); 
  Thread_specific_ptr (void (* cleanup) (void *)); 
  ~ Thread_specific_ptr (); 

  / / Modifier functions 
  T * release (); 
  Void reset (T * = 0); 

  / / Observer functions 
  T * get () const; 
  T * operator -> () const; 
  T & T & operator *()() const; 
  ); 
  Can get support, reset, release, and other operations. 
  Thread_specific_ptr the realization of a very simple, just to be tss category of "modified" look into intelligence indicators such, in its constructor function will automatically create a tss object, but in the Analysis of the structure function will be called to reset the default parameter function, thus leading to the internal been packaged configuration tss objects are out to "automatic" memory allocation release management purposes. 

  Following is a realization use thread_specific_ptr TSS examples: 
  # Include <boost/thread/thread.hpp> 
  # Include <boost/thread/mutex.hpp> 
  # Include <boost/thread/tss.hpp> 
  # Include <iostream> 

  Boost:: mutex io_mutex; 
  Boost:: thread_specific_ptr <int> ptr; / / use this method to tell that this member will not shared by all threads 

  Struct count 
  ( 
  Count (int id): id (id) () 

  Void operator () () 
  ( 
  If (ptr. Get () == 0) / / if ptr is not initialized, initialize it 
  Ptr. Reset (new int (0)) / / Attention, we pass a pointer to reset (actually set ptr) 

  For (int i = 0; i <10; + + i) 
  ( 
  (* Ptr) + +; 
  Boost:: mutex:: scoped_lock lock (io_mutex); 
  Std:: cout <<id << ":" <<* ptr <<std:: endl; 
  ) 
  ) 

  Int id; 
  ); 

  Int main (int argc, char * argv []) 
  ( 
  Boost:: thread thrd1 (count (1)); 
  Boost:: thread thrd2 (count (2)); 
  Thrd1. Join (); 
  Thrd2. Join (); 

  Return 0; 
  ) 

  In addition, the thread also provided a very interesting function, call_once in tss:: init used on the realization of the function. 
  The function of the statement is as follows: 
  Void call_once (void (* func) (), once_flag & once_flag & flag); 
  The function of Windows to achieve through the creation of a Mutex all threads in trying to implement the function of the state at the time of waiting until the end of a thread implementation func function, the function's second parameter function func said it has been implemented, the often initialization parameters into BOOST_ONCE_INIT (0), if you initialize the parameters into one, the function func will not be called at this time call_once equivalent to what has not stem, which sometimes may be needed, for example, , according to the results of procedures need to decide whether or not a function func call_once. 
  Call_once function in the implementation of End func, will be revised to a flag, it will lead to the future implementation of call_once threads (including pending in the Mutex threads and the threads just entering call_once) func will skip the implementation of the code. 

  It must be noted that this function is not a template function, but an ordinary function, which has one of the first parameter is a function pointer, the type (*)() void, and not to boost the library with many other places in the is the template function, but no such relationship, a boost:: bind this super weapons, how to bind the parameters as you will, in accordance with the document boost for the introduction of abnormal function can not be thrown out, but Implementation of the code seems not the case. 

  The following is a typical application of achieving a call_once initialization examples: 
  # Include <boost/thread/thread.hpp> 
  # Include <boost/thread/once.hpp> 
  # Include <iostream> 

  Int i = 0; 
  Int j = 0; 
  Boost:: = BOOST_ONCE_INIT once_flag flag; 

  Void init () 
  ( 
  + + I; 
  ) 

  Void thread () 
  ( 
  Boost:: call_once (init & & init, flag); 
  + + J; 
  ) 

  Int main (int argc, char * argv []) 
  ( 
  Boost:: thread thrd1 (& thread & thread); 
  Boost:: thread thrd2 (& thread & thread); 
  Thrd1. Join (); 
  Thrd2. Join (); 

  Std:: cout <<i <<std:: endl; 
  Std:: cout <<j <<std:: endl; 

  Return 0; 
  ) 
  The results showed that the overall situation was only variable i + + implementation of an operation, while variable j in the two threads in both the implementation of the + + operator. 

  Other 
  Boost:: thread is not yet perfect, the main issues include: no thread priority support, or support for the abolition of threading operation, and the current mechanism to achieve it would not be easy to achieve this through simple modification request Perhaps a future version will be realized on a larger adjustment, but now supports the interface should be relatively stable, and also the characteristics of the current support will continue to be effective. 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply