Saturday, 2 April 2016

c++ - Using __thread in c++0x



I read that there was a new keyword in C++: it's __thread from what I've read.



All I know is that it's a keyword to be used like the static keyword but I know nothing else. Does this keyword just mean that, for instance, if a variable were declared like so:




__thread int foo;


then anything to do with that variable will be executed with a new thread?


Answer



It's thread_local, not __thread. It's used to define variables which has storage duration of the thread.



thread_local is a new storage duration specifier added in C++0x. There are other storage duration : static, automatic and dynamic.



From this link:





thread local storage duration (C++11 feature). The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration.







I think the introduction of this keyword was made possible by introducing a standardized memory model in C++0x:





No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...