Post

【C++】thread_local变量

在C++多线程环境下,全局变量、局部变量、静态变量和thread_local变量的区别如下:

 全局变量局部变量静态变量thread_local变量
存储期程序运行期间函数调用期间程序运行期间线程运行期间
线程安全××

示例程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <thread>

int g = 0;

void global_test() {
    std::cout << &g << '\n';
    for (int i = 0; i < 10000; ++i) ++g;
    std::cout << g << '\n';
}

void local_test() {
    int n = 0;
    std::cout << &n << '\n';
    for (int i = 0; i < 10000; ++i) ++n;
    std::cout << n << '\n';
}

void static_test() {
    static int n = 0;
    std::cout << &n << '\n';
    for (int i = 0; i < 10000; ++i) ++n;
    std::cout << n << '\n';
}

void thread_local_test() {
    thread_local int n = 0;
    std::cout << &n << '\n';
    for (int i = 0; i < 10000; ++i) ++n;
    std::cout << n << '\n';
}

int main() {
    for (auto f : {global_test, local_test, static_test, thread_local_test}) {
        std::thread t1(f), t2(f);
        t1.join();
        t2.join();
    }
    return 0;
}

可能的输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
0x10b7fb01c
0x10b7fb01c
10148
19117
0x7000078cdf2c
0x700007950f2c
10000
10000
0x108d0001c
0x108d0001c
15854
15936
0x600001008000
0x600001004050
10000
10000

参考

This post is licensed under CC BY 4.0 by the author.