Make a little progress every day thread-local storage in--linux (ii)

Source: Internet
Author: User
Tags terminates

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:

    1. Assume that the variable declaration uses a quantity of keywordstatic or extern. Then Keyword__thread must be followed.
    2. 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.
    3. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.