Many programs are not designed to be re-entangled. This function must be careful when using it, especially in the case of multi-thread. Because when other functions are being toned
When using this function, you call this function again, then the result that is likely to get is unpredictive. Under normal circumstances, if this function uses a static data.
Such procedures are generally irrelevant. For example, std ::stok is an unrebeiled function because it uses static data to save String of Token. An unreachable program to be converted into a reusable program generally be implemented by two methods. One way is to modify the interface of the program means modifying a lot of code. Another method is to do not change the interface but the static store is stored as a process partial storage. Have
Also called Thread-Specific Storage.
THREAD local storage is related to a specific thread. Multi-thread libraries give interface access to these local storage. This makes it possible to get reincarnation. But thread access
Thread Locak's storage is more than accessible or partial objects. But this is also a way to solve it. This is also under the premise of do not change the interface.
Unique solution.
Boost.Thread accesses Thread Local storage via smart pointer boost :: thread_specific_ptr. Initialize the pointer first when you are using.
Boost.Thread ensures that it is automatically cleaned up when Thread Local Storage exits the thread in Thread. Here is an example: #include
Boost :: Mutex IO_MUTEX; boost :: thread_specific_ptr
Struct Count {Count (INT ID): ID (ID) {}
Void Operator () () {if (ptr.get () == 0) Ptr.reset (new int (0));
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; thrd1.join (); thrd2.join (); Return }
There is also something that needs to be processed, how to ensure that the initialization program is Thread-Safe. #include
INT i = 0; boost :: overcE_flag flag = boost_once_init;
Void init () { i;}
Void thread () {boost :: Call_once (& Init, flag);