問題描述
我對 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.
它添加到當前自動(存在于塊/函數期間)、靜態(存在于程序持續時間)和動態(存在于分配和釋放之間的堆上).
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 是什么意思?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!