我读到 C++ 中有一个新关键字:它是我读过的 __thread
。
我只知道它是一个可以像 static
关键字一样使用的关键字,但我什么都不知道。这个关键字是否仅仅意味着,例如,如果一个变量是这样声明的:
__thread int foo;
那么与该变量有关的任何事情都将使用新线程执行?
最佳答案
它是 thread_local
,而不是 __thread
。用于定义具有线程存储时长的变量。
thread_local
是在 C++0x 中添加的 new 存储持续时间说明符。还有其他存储时长:static、automatic和dynamic。
来自 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.
我认为这个关键字的引入是通过在 C++0x 中引入标准化的内存模型来实现的:
关于c++ - 在 c++0x 中使用 __thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047226/