Reprint please indicate source: http://blog.csdn.net/cywosp/article/details/26876231
Another more efficient method of thread-local storage in Linux is to use Keyword__thread to define variables. __thread is a built-in thread-local storage facility for GCC (thread-local Storage), which is very efficient to implement. It is more high speed than the pthread_key_t. Its storage performance is comparable to global variables and is easier to use. Creating a thread local variable simply adds the __thread description to the declaration of a global or static variable. columns such as: static __thread char t_buf[32] = {'} '}; extern __thread int t_val = 0;any variable with __thread, each thread has a copy of the variable. Without interfering with each other. Variables in thread-local storage persist until the thread terminates. This storage is released on its own initiative when the thread terminates. __thread is not available for all data types. Because it only supports pods (Plain old data structure)[1]Type. Class type is not supported--it cannot invoke constructors and destructors on its own initiative.
At the same time __thread can be used to modify global variables, static variables within functions, but cannot be used to modify local variables of functions or ordinary member variables of class. Other than that. The initialization of the __thread variable can only be used for compile-time constants. Like what:__thread std::string t_object_1 ("Swift"); //error. Because the constructor of the object cannot be called __thread std::string* t_object_2 = new std::string (); //error, initialization must be with compile-time constants __thread std::string* t_object_3 = nullptr; //correct, but requires manual initialization and destruction of objects
In addition to the above. There are several points to note about the declaration and use of thread-local storage variables:
- Assume that the variable declaration uses a quantity of keywordstatic or extern. Then Keyword__thread must be followed.
- Is the same as a general global variable or static variable. A thread-local variable is able to set an initialization value at the time of declaration.
- The ability to use the C-language address character (&) to obtain the address of a thread-local variable.
The use of __thread sample can be used to test the implementation of HTTPS://GITHUB.COM/APUSAPP/SWIFT/BLOB/MASTER/SWIFT/BASE/LOGGING.CPP and its unit test for those non-pod data types. Suppose you want to use a thread-local storage mechanism. Can be handled using classes that are encapsulated in the pthread_key_t. A detailed approach to the implementation of Https://github.com/ApusApp/Swift/blob/master/swift/base/threadlocal.h and its unit test
References[1] http://zh.wikipedia.org/wiki/POD_ (%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1)[2] Linux/unix System Programming Manual (UP)[3] Linux multi-threaded server programming using Muduo C + + network library
Make a little progress every day thread-local storage in--linux (ii)