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

相关资源
高性能存储虚拟化方案 NVMe over Fabric 在火山引擎的演进
在云计算中,虚拟化存储扮演着重要角色,其中 iSCSI 协议在业界开放、流行多年。近年来,拥有更优性能的 NVMe over Fabrics 协议也得到了发展。本次分享介绍了 NVMe over Fabrics 在云原生和虚拟化方向的演进工作和成果。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论