Home >Backend Development >C++ >Are thread_local Variables Implicitly Static in Block Scope?

Are thread_local Variables Implicitly Static in Block Scope?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 03:35:02483browse

Are thread_local Variables Implicitly Static in Block Scope?

Are Variables Declared with thread_local Automatically Static?

The following code segments appear identical at a glance:

<code class="cpp">void f() {
    thread_local vector<int> V;
    // ...
}

void f() {
    static thread_local vector<int> V;
    // ...
}</code>

However, the C Standard reveals a subtle difference. When defining a thread_local variable with block scope, the static storage class specifier is implied if not explicitly stated. This means that the first code segment is equivalent to the second.

Despite their similar definitions, static and thread_local variables differ significantly. Variables with static storage duration are associated with the entire program, while thread_local variables are associated with individual threads. Each thread has its own distinct object or reference associated with the thread_local variable.

The above is the detailed content of Are thread_local Variables Implicitly Static in Block Scope?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn