c++ - 在 c++0x 中使用 __thread

标签 c++ multithreading c++11

我读到 C++ 中有一个新关键字:它是我读过的 __thread

我只知道它是一个可以像 static 关键字一样使用的关键字,但我什么都不知道。这个关键字是否仅仅意味着,例如,如果一个变量是这样声明的:

__thread int foo;

那么与该变量有关的任何事情都将使用新线程执行?

最佳答案

它是 thread_local,而不是 __thread。用于定义具有线程存储时长的变量。

thread_local 是在 C++0x 中添加的 new 存储持续时间说明符。还有其他存储时长:staticautomaticdynamic

来自 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/

相关文章:

c - 无法使用 pthread_cond_signal 向 C 中的另一个进程发送信号

c++ - deleteLater() 是否等待所有挂起的信号被传递?

c# - 线程同步。为什么这个锁不足以同步线程

c++ - clang++ 是否以更宽松的方式对待系统头?

c# - 我不了解 volatile 和 Memory-Barrier 的是

c++ - 这个 "return ~Result(0)"在 C++ 中是什么意思?

c++ - 可以在声明它们的命名空间之外定义类成员吗?

c++ - 在 C++ unix 中异步写入文件

c++ - 如何使用 umdh.exe 在所有堆中跟踪内存泄漏?

c++ - Qt 5.7 将 -std=gnu++11 添加到我的编译器标志,破坏 -std=c++14