C++11:thread_local

Source: Internet
Author: User

The thread_local variable is a new storage type introduced by C + + 11. It affects the storage period of the variable (Storage Duration) and there are 4 storage cycles in C + +:

    1. Automatic
    2. Static
    3. Dynamic
    4. Thread

Variables that have and are only thread_local keyword modifiers have thread cycles (thread duration), which are generated (or allocated) when the thread starts, and are destroyed (deallocated) at the end of the Thread. And each thread possesses a separate instance of the variable (each of the thread have its own instance of the object). thread_localcan be static extern used in conjunction with the keyword, which will affect the link properties of the variable (to adjust linkage).

so, What variables can be declared as thread_local? The following 3 classes are OK

    1. Global variables under namespaces
    2. Static member variable of class
    3. Local variables
The following examples from the C + + Concurrency in action book illustrate these 3 scenarios: thread_localint x;//A thread-local variable at namespace scope
Class X
{
static thread_local std::string s;  //a thread-local static class  data member
};
static thread_local std::string x:: S;  //the definition of x :: S is required

void foo ()
{
    thread_local std::vector<int> v ;   //a thread-local local  variable
}

Since each thread has a separate thread_local variable, there are 2 issues to consider:

    1. How the thread_local variable of each thread is initialized
    2. The life cycle of the thread_local variables of each thread after initialization, especially local variables declared as thread_local (locally variables)

The following code can help answer these 2 questions, my test environment is vs2015.

The first 3 rows of the output can help to answer how the thread_local variable is initialized, and you can see that each thread is initialized one at a time, and that the g_n in the example is initially initialized to 1 in the main thread and subsequently modified to 2 and 3, but these modifications do not affect g_ n the initial values in thread T2 and T3 (value 1), Although the values of the variables in the main thread are updated to 3 when the T2 and T3 threads start, the main thread, thread1, and thread2 print results are 3,2,2 respectively.

The last 6 lines of print illustrate the fact that local variables declared as thread_local persist in the thread, unlike the life cycle of a normal temporary variable, which has the same initialization characteristics and life cycle as a static variable, although it is not declared Static. In the example, the thread_local variable i in the Foo function is initialized at the first execution of each thread and is released at the end of each thread.

#include <thread>

Thread_localint g_n =1;

void F ()
{
g_n++;
printf"id=%d, n=%d\n", std::this_thread::get_id (), g_n);
}

void Foo ()
{
Thread_localint i=0;
printf"id=%d, n=%d\n", std::this_thread::get_id (), i);
i++;
}

void F2 ()
{
Foo ();
Foo ();
}

int main ()
{
g_n++;
F ();
Std::thread T1 (f);
Std::thread T2 (f);

T1.join ();
T2.join ();


F2 ();
std::thread T4 (f2);
Std::thread T5 (f2);

T4.join ();
T5.join ();
return 0;
}

Output (the ID value is time-varying per run):

id=8004, n=3

id=8008, n=2
id=8012, n=2
id=8004, n=0
id=8004, N=1
id=8016, n=0
id=8016, N=1
id=8020, n=0
id=8020, n=1 from:http://www.cnblogs.com/pop-lar/p/5123014.html

C++11:thread_local

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.