在C ++ 0x中使用__thread [英] Using __thread in c++0x
问题描述
我了解到C ++中有一个新关键字:根据我的阅读,它是 __ thread
.
I read that there was a new keyword in C++: it's __thread
from what I've read.
我所知道的是,它是一个与 static
关键字一样使用的关键字,但我一无所知.例如,如果像这样声明一个变量,此关键字是否只是意味着:
All I know is that it's a keyword to be used like the static
keyword but I know nothing else. Does this keyword just mean that, for instance, if a variable were declared like so:
__thread int foo;
那么与该变量有关的任何事情将在新线程中执行吗?
then anything to do with that variable will be executed with a new thread?
推荐答案
它是 thread_local
,而不是 __ thread
.它用于定义具有线程存储持续时间的变量.
It's thread_local
, not __thread
. It's used to define variables which has storage duration of the thread.
thread_local
是C ++ 0x中添加的新存储持续时间说明符.还有其他存储期限:静态,自动和动态.
thread_local
is a new storage duration specifier added in C++0x. There are other storage duration : static, automatic and dynamic.
来自此链接:
本地线程存储持续时间(C ++ 11功能).该变量在线程开始时分配,并在线程结束时释放.每个线程都有其自己的变量实例.只有声明为thread_local的变量才具有此存储期限.
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中引入标准化的内存模型来引入此关键字:
I think the introduction of this keyword was made possible by introducing a standardized memory model in C++0x:
这篇关于在C ++ 0x中使用__thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!