C ++ 11中的thread_local是什么意思? [英] What does the thread_local mean in C++11?

查看:241
本文介绍了C ++ 11中的thread_local是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++ 11中对thread_local的描述感到困惑。我的理解是,每个线程都有一个函数中的局部变量的唯一副本。全局/静态变量可以被所有线程访问(可能使用锁的同步访问)。和thread_local变量是可见的所有线程,但只能修改由他们定义的线程?这是对的吗?

I am confused with the description of thread_local in C++11. My understanding is, each thread has unique copy of local variables in a function. The global/static variables can be accessed by all the threads (possibly synchronized access using locks). And the thread_local variables are visible to all the threads but can only modified by the thread for which they are defined? Is it correct?

推荐答案

线程本地存储持续时间是用于指代看起来是全局或静态存储持续时间的数据

Thread-local storage duration is a term used to refer to data that is seemingly global or static storage duration (from the viewpoint of the functions using it) but in actual fact, there is one copy per thread.

它添加到当前的自动(在块/函数中存在),static()存在于程序持续时间)和动态(存在于分配和释放之间的堆上)。

It adds to the current automatic (exists during a block/function), static (exists for the program duration) and dynamic (exists on the heap between allocation and deallocation).

在线程创建时处理线程本地当线程停止时。

Something that is thread-local is brought into existence at thread creation and disposed of when the thread stops.

以下是一些例子。

想想一个随机数生成器,维护在每个线程的基础上。使用线程本地种子意味着每个线程都有自己的随机数序列,与其他线程无关。

Think of a random number generator where the seed must be maintained on a per-thread basis. Using a thread-local seed means that each thread gets its own random number sequence, independent of other threads.

如果你的种子是随机函数中的局部变量,将在每次调用它时初始化,每次给你相同的数字。如果它是一个全局,线程会干扰对方的序列。

If your seed was a local variable within the random function, it would be initialised every time you called it, giving you the same number each time. If it was a global, threads would interfere with each other's sequences.

另一个例子是 strtok 令牌化状态存储在线程特定的基础上。这样,单个线程可以确保其他线程不会拧紧它的标记化努力,同时仍然能够通过对 strtok 的多个调用保持状态 - 这基本上呈现 strtok_r (线程安全版本)冗余。

Another example is something like strtok where the tokenisation state is stored on a thread-specific basis. That way, a single thread can be sure that other threads won't screw up its tokenisation efforts, while still being able to maintain state over multiple calls to strtok - this basically renders strtok_r (the thread-safe version) redundant.

这两个例子允许线程局部变量存在 使用它的函数。在预线程代码中,它只是函数内的静态存储持续时间变量。对于线程,修改为线程本地存储持续时间。

Both these examples allow for the thread local variable to exist within the function that uses it. In pre-threaded code, it would simply be a static storage duration variable within the function. For threads, that's modified to thread local storage duration.

另一个例子是 errno 。在您的一个调用失败之后,但是您可以检查变量,但每个线程只需要一个副本,您不希望单独的线程修改 errno

Yet another example would be something like errno. You don't want separate threads modifying errno after one of your calls fails but before you can check the variable, and yet you only want one copy per thread.

此网站对不同的存储时间有合理的描述

This site has a reasonable description of the different storage duration specifiers.

这篇关于C ++ 11中的thread_local是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆