Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C++11 thread_local variables automatically static?

Is there a difference between these two code segments:

void f() {     thread_local vector<int> V;     V.clear();     ... // use V as a temporary variable } 

and

void f() {     static thread_local vector<int> V;     V.clear();     ... // use V as a temporary variable } 

Backstory: originally I had a STATIC vector V (for holding some intermediate values, it gets cleared every time I enter the function) and a single-threaded program. I want to turn the program into a multithreading one, so somehow I have to get rid of this static modifier. My idea is to turn every static into thread_local and not worry about anything else? Can this approach backfire?

like image 276
Zuza Avatar asked Apr 01 '14 18:04

Zuza


People also ask

When all local variables are static?

A local static variable is a variable, whose lifetime doesn't stop with a function call where it is declared. It extends until the lifetime of a complete program. All function calls share the same copy of local static variables. These variables are used to count the number of times a function is called.

Can static variable be declared multiple times?

Static variables can be assigned as many times as you wish.

Can we redefine static variable in C?

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name. Static variables are initialized only once.

What is static Variale in C++?

A static variable is a variable that is declared using the keyword static. The space for the static variable is allocated only one time and this is used for the entirety of the program. Once this variable is declared, it exists till the program executes.


2 Answers

According to the C++ Standard

When thread_local is applied to a variable of block scope the storage-class-specifier static is implied if it does not appear explicitly

So it means that this definition

void f() {     thread_local vector<int> V;     V.clear();     ... // use V as a temporary variable } 

is equivalent to

void f() {     static thread_local vector<int> V;     V.clear();     ... // use V as a temporary variable } 

However, a static variable is not the same as a thread_local variable.

1 All variables declared with the thread_local keyword have thread storage duration. The storage for these entities shall last for the duration of the thread in which they are created. There is a distinct object or reference per thread, and use of the declared name refers to the entity associated with the current thread

To distinguish these variables the standard introduces a new term thread storage duration along with static storage duration.

like image 199
Vlad from Moscow Avatar answered Oct 05 '22 18:10

Vlad from Moscow


Yes, "thread-local storage" is very similar to "global" (or "static storage"), only that instead of "duration of the entire program" you have "duration of the entire thread". So a block-local thread-local variable is initialized the first time control passes through its declaration, but separately within each thread, and it's destroyed when the thread ends.

like image 28
Kerrek SB Avatar answered Oct 05 '22 19:10

Kerrek SB