久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++11 中的 thread_local 是什么意思?

What does the thread_local mean in C++11?(C++11 中的 thread_local 是什么意思?)
本文介紹了C++11 中的 thread_local 是什么意思?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我對 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進(jìn)行排序)
Rotating a point about another point (2D)(圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)一個(gè)點(diǎn) (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時(shí)間?)
Sort list using STL sort function(使用 STL 排序功能對列表進(jìn)行排序)
主站蜘蛛池模板: 欧美午夜精品久久久久久浪潮 | 亚洲国产精品久久久 | 中国大陆高清aⅴ毛片 | 美人の美乳で授乳プレイ | 国产精品人人做人人爽 | 国产美女精品视频 | 91偷拍精品一区二区三区 | 亚洲成人久久久 | 国产精品久久久久久久久久久久久 | 青青草一区二区 | 免费观看一级特黄欧美大片 | 亚洲国产精品一区 | 日韩免费视频一区二区 | 欧美精品一区二区三区在线 | 久久麻豆精品 | 午夜大片 | 一二三四在线视频观看社区 | 亚洲在线一区二区 | 韩国av一区二区 | 91视频.com | 日韩欧美高清 | 91在线看网站 | 色综久久 | 日本不卡免费新一二三区 | 高清一区二区三区 | 国产精品一二三区 | 国产精品精品视频一区二区三区 | 91av亚洲 | 国产美女在线看 | 欧美国产日本一区 | 国产精品欧美日韩 | 国产一级片精品 | 欧美一级免费 | 都市激情亚洲 | 亚洲成人精品影院 | 久久久91精品国产一区二区三区 | 精品一区二区三区四区外站 | 日本在线看片 | www国产亚洲精品久久网站 | 九九热在线免费视频 | 日韩视频一区二区 |