pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

What does the thread_local mean in C++11?(C++11 中的 thread_local 是什么意思?)
本文介紹了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.

它添加到當前自動(存在于塊/函數期間)、靜態(存在于程序持續時間)和動態(存在于分配和釋放之間的堆上).

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模板網!

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

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 彼得逊采泥器-定深式采泥器-电动土壤采样器-土壤样品风干机-常州索奥仪器制造有限公司 | 气动隔膜阀_气动隔膜阀厂家_卫生级隔膜阀价格_浙江浙控阀门有限公司 | 甲级防雷检测仪-乙级防雷检测仪厂家-上海胜绪电气有限公司 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 氧化铁红厂家-淄博宗昂化工| 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 复合土工膜厂家|hdpe防渗土工膜|复合防渗土工布|玻璃纤维|双向塑料土工格栅-安徽路建新材料有限公司 | 广东泵阀展|阀门展-广东国际泵管阀展览会 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 成都装修公司-成都装修设计公司推荐-成都朗煜装饰公司 | 湖南自考_湖南自学考试网 | 气动球阀_衬氟蝶阀_调节阀_电动截止阀_上海沃托阀门有限公司 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | POS机办理_个人pos机免费领取-银联pos机申请首页 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 电液推杆生产厂家|电动推杆|液压推杆-扬州唯升机械有限公司 | 河北码上网络科技|邯郸小程序开发|邯郸微信开发|邯郸网站建设 | 国际高中-国际学校-一站式择校服务-远播国际教育 | 散热器厂家_暖气片_米德尔顿散热器 | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 深圳货架厂家_金丽声精品货架_广东金丽声展示设备有限公司官网 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | 万家财经_财经新闻_在线财经资讯网 | 宽带办理,电信宽带,移动宽带,联通宽带,电信宽带办理,移动宽带办理,联通宽带办理 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 | 单螺旋速冻机-双螺旋-流态化-隧道式-食品速冻机厂家-广州冰泉制冷 | 精密五金加工厂-CNC数控车床加工_冲压件|蜗杆|螺杆加工「新锦泰」 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 | 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 重庆网站建设,重庆网站设计,重庆网站制作,重庆seo,重庆做网站,重庆seo,重庆公众号运营,重庆小程序开发 | SMC-ASCO-CKD气缸-FESTO-MAC电磁阀-上海天筹自动化设备官网 |