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

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

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

問(wèn)題描述

我對(duì) C++11 中 thread_local 的描述感到困惑.我的理解是,每個(gè)線程在函數(shù)中都有唯一的局部變量副本.所有線程都可以訪問(wèn)全局/靜態(tài)變量(可能使用鎖進(jìn)行同步訪問(wèn)).thread_local 變量對(duì)所有線程都是可見(jiàn)的,但只能由為其定義的線程修改?這是正確的嗎?

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?

推薦答案

線程局部存儲(chǔ)持續(xù)時(shí)間是一個(gè)術(shù)語(yǔ),用于指看似全局或靜態(tài)的存儲(chǔ)持續(xù)時(shí)間(從使用它的函數(shù)的角度來(lái)看)但在實(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)前自動(dòng)(存在于塊/函數(shù)期間)、靜態(tài)(存在于程序持續(xù)時(shí)間)和動(dòng)態(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è)局部變量,它會(huì)在你每次調(diào)用它時(shí)被初始化,每次都給你相同的數(shù)字.如果是全局的,線程會(huì)干擾彼此的序列.

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è)示例類(lèi)似于 strtok,其中標(biāo)記化狀態(tài)存儲(chǔ)在特定于線程的基礎(chǔ)上.這樣,單個(gè)線程可以確保其他線程不會(huì)破壞其標(biāo)記化工作,同時(shí)仍然能夠通過(guò)多次調(diào)用 strtok 來(lái)維護(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)存儲(chǔ)持續(xù)時(shí)間變量.對(duì)于線程,修改為線程本地存儲(chǔ)持續(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)站對(duì)不同的存儲(chǔ)期限說(shuō)明符進(jìn)行了合理的描述.

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

這篇關(guān)于C++11 中的 thread_local 是什么意思?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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++ 中對(duì)壓縮(鎖定)容器進(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 識(shí)別的算法改進(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 排序功能對(duì)列表進(jìn)行排序)
主站蜘蛛池模板: 中文字幕av一区二区 | 午夜在线国语中文字幕视频 | 欧洲精品一区二区 | 国产激情视频在线 | 在线黄色网 | 亚洲永久免费视频 | 国产精品国产三级国产aⅴ浪潮 | 九九在线观看高清免费 | www.少妇 | 福利视频网址导航 | 91精品国产综合久久久久久 | 成人福利在线观看 | 国产成人在线播放 | 国产又粗又大又硬 | 一区二区在线视频 | 超碰在线中文字幕 | 亚洲综合精品 | 日韩999 | 精品视频在线观看免费 | 国产精品不卡 | 激情综合五月婷婷 | 天天天天躁天天爱天天碰2018 | 久久国产小视频 | 亚洲精品一区二区三区精华液 | 日本黄色三级视频 | 天天干少妇 | 日韩成人小视频 | 黄色片在线| 中文字幕av片 | av手机版| 97精品在线 | 成人av一区二区三区在线观看 | 在线视频一区二区三区 | 欧美在线网站 | 免费看黄色av | 免费网站av | 色婷av| 国产xxx| 在线观看黄色小视频 | 国产精品久久久久久久久 | 国产在线视频网站 |