問題描述
我對 C++11 中 thread_local
的描述感到困惑.我的理解是,每個(gè)線程在函數(shù)中都有唯一的局部變量副本.所有線程都可以訪問全局/靜態(tài)變量(可能使用鎖進(jìn)行同步訪問).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?
推薦答案
線程局部存儲持續(xù)時(shí)間是一個(gè)術(shù)語,用于指看似全局或靜態(tài)的存儲持續(xù)時(shí)間(從使用它的函數(shù)的角度來看)但在實(shí)際上,每個(gè)線程只有一個(gè)副本.
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.
它添加到當(dāng)前自動(存在于塊/函數(shù)期間)、靜態(tài)(存在于程序持續(xù)時(shí)間)和動態(tài)(存在于分配和釋放之間的堆上).
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).
線程本地的東西在線程創(chuàng)建時(shí)就存在,并在線程停止時(shí)處理.
Something that is thread-local is brought into existence at thread creation and disposed of when the thread stops.
下面是一些例子.
考慮一個(gè)隨機(jī)數(shù)生成器,其中必須在每個(gè)線程的基礎(chǔ)上維護(hù)種子.使用線程本地種子意味著每個(gè)線程都有自己的隨機(jī)數(shù)序列,獨(dú)立于其他線程.
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.
如果你的種子是隨機(jī)函數(shù)中的一個(gè)局部變量,它會在你每次調(diào)用它時(shí)被初始化,每次都給你相同的數(shù)字.如果是全局的,線程會干擾彼此的序列.
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.
另一個(gè)示例類似于 strtok
,其中標(biāo)記化狀態(tài)存儲在特定于線程的基礎(chǔ)上.這樣,單個(gè)線程可以確保其他線程不會破壞其標(biāo)記化工作,同時(shí)仍然能夠通過多次調(diào)用 strtok
來維護(hù)狀態(tài) - 這基本上呈現(xiàn) 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.
這兩個(gè)例子都允許線程局部變量存在于使用它的函數(shù)中.在預(yù)線程代碼中,它只是函數(shù)內(nèi)的靜態(tài)存儲持續(xù)時(shí)間變量.對于線程,修改為線程本地存儲持續(xù)時(shí)間.
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.
還有一個(gè)例子,比如errno
.您不希望在您的一次調(diào)用失敗后但在檢查變量之前修改 errno
的單獨(dú)線程,但您只需要每個(gè)線程一個(gè)副本.
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.
本網(wǎng)站對不同的存儲期限說明符進(jìn)行了合理的描述.
This site has a reasonable description of the different storage duration specifiers.
這篇關(guān)于C++11 中的 thread_local 是什么意思?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!