C++11

C++11

左值与右值

  • 可以取地址的,有名字的,非临时的就是左值

  • 不能取地址的,没有名字的,临时的就是右值

thread_local

thread_local 变量是 C++ 11 新引入的一种存储类型,这些变量(或者说对象)在线程开始的时候被生成(allocated),在线程结束的时候被销毁(deallocated)。并且每 一个线程都拥有一个独立的变量实例。thread_local 可以和staticextern关键字联合使用,这将影响变量的链接属性(to adjust linkage)。

Thread_local 变量使用范围:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
thread_local int x;  //命名空间下的全局变
class X
{
    static thread_local std::string s; //A thread-local static class data member
};
static thread_local std::string X::s;  //The definition of X::s is required

void foo()
{
    thread_local std::vector<int> v;  //A thread-local local variable
}

参考

  1. https://www.cnblogs.com/pop-lar/p/5123014.html
  2. decltype 说明符 - cppreference.com
updatedupdated2024-05-102024-05-10