C++ thread_local 关键字

向量数据库大模型云通信

thread_local 关键字用于控制 name 的 storage durationlinkage. 其属性如下:

**thread_local** - thread storage duration.

thread_local 关键字可以和 staticextern 一起出现 (since C++11)。

  • thread_local 关键字只能用于 namespace scope 中的对象声明,block scope 中的对象声明 和 static 数据成员。
  • 指明对象具有 thread 存储周期。
  • 当只有 thread_local 修饰 block scope 变量时,该变量隐士包含 static 语义。
  • thread_local可以和 staticextern一同使用,分别表示 internal 或 external linkage (除了 static 数据成员,它总是 external linkage)。

thread_local 存储周期

thread 存储周期的对象在线程开始时分配,在线程结束时释放。每个线程具有独自的对象。

thread storage duration. The storage for the object is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the object. Only objects declared thread_local have this storage duration. thread_local can appear together with static or extern to adjust linkage.

See Non-local variables and Static local variables for details on initialization of objects with this storage duration.

Non-local variables

All non-local variables with thread-local storage duration are initialized as part of thread launch, sequenced-before the execution of the thread function begins.

全局 thread-local 对象的初始化是在线程launch时,开始执行指令前。

Static local variables

Variables declared at block scope with the specifier static or thread_local (since C++11) have static or thread (since C++11) storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

local thread_local 对象初始化是在第一次调用、或使用时初始化。和static的使用相同,我们后面看下是否单例模式中也有应用。

参考

0
0
0
0
关于作者

文章

0

获赞

0

收藏

0

相关资源
CloudWeGo白皮书:字节跳动云原生微服务架构原理与开源实践
本书总结了字节跳动自2018年以来的微服务架构演进之路
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论