C++11多线程-线程局部存储(thread_local)

来源:https://www.jianshu.com/p/8df45004bbcb,https://blog.csdn.net/zzhongcy/article/details/91372329

C++存储类型

线程局部存储在其它语言中都是以库的形式提供的(库函数或类)。但在C++11中以关键字的形式,做为一种存储类型出现,由此可见C++11对线程局部存储的重视。C++11中有如下几种存储类型:

序号 类型 备注
1 auto right-aligned 该关键字用于两种情况:1. 声明变量时, 根据初始化表达式自动推断变量类型。2. 声明函数作为函数返回值的占位符。
2 static static变量只初始化一次,除此之外它还有可见性的属性:1. static修饰函数内的“局部”变量时,表明它不需要在进入或离开函数时创建或销毁。且仅在函数内可见。2. static修饰全局变量时,表明该变量仅在当前(声明它的)文件内可见。3. static修饰类的成员变量时,则该变量被该类的所有实例共享。
3 register 寄存器变量。该变量存储在CPU寄存器中,而不是RAM(栈或堆)中。该变量的最大尺寸等于寄存器的大小。由于是存储于寄存器中,因此不能对该变量进行取地址操作。
4 extern 引用一个全局变量。当在一个文件中定义了一个全局变量时,就可以在其它文件中使用extern来声明并引用该变量。
5 mutable 仅适用于类成员变量。以mutable修饰的成员变量可以在const成员函数中修改。参见上一章chan.simple.h中对mutex的使用。
6 thread_local 线程周期。

thread_local修饰的变量具有如下特性:

  1. 变量在线程创建时生成(不同编译器实现略有差异,但在线程内变量第一次使用前必然已构造完毕)。
  2. **线程结束时被销毁(**析构,利用析构特性,thread_local变量可以感知线程销毁事件)。
  3. 每个线程都拥有其自己的变量副本
  4. thread_local可以和static或extern联合使用,这将会影响变量的链接属性。

下面代码演示了thread_local变量在线程中的生命周期

// thread_local.cpp
#include <iostream>
#include <thread>

class A {
public:
  A() {
    std::cout << std::this_thread::get_id()
              << " " << __FUNCTION__
              << "(" << (void *)this << ")"
              << std::endl;
  }

  ~A() {
    std::cout << std::this_thread::get_id()
              << " " << __FUNCTION__
              << "(" << (void *)this << ")"
              << std::endl;
  }

  // 线程中,第一次使用前初始化
  void doSth() {
  }
};

thread_local A a;

int main() {
  a.doSth();
  std::thread t([]() {
    std::cout << "Thread: "
              << std::this_thread::get_id()
              << " entered" << std::endl;
    a.doSth();
  });

  t.join();

  return 0;
}

运行该程序

$> g++ -std=c++11 -o debug/tls.out ./thread_local.cpp
$> ./debug/tls.out
01 A(0xc00720)
Thread: 02 entered
02 A(0xc02ee0)
02 ~A(0xc02ee0)
01 ~A(0xc00720)
$>

变量a在main线程和t线程中分别保留了一份副本,以下时序图表明了两份副本的生命周期。

在这里插入图片描述
哪些变量可以被声明为thread_local?

  1. 命名空间下的全局变量
  2. 类的static成员变量
  3. 本地变量

既然每个线程都拥有一份独立的thread_local变量,那么就有2个问题需要考虑:
4. 各线程的thread_local变量是如何初始化的
5. 各线程的thread_local变量在初始化之后拥有怎样的生命周期,特别是被声明为thread_local的本地变量(local variables)

下面的代码可以帮助回答这2个问题

#include <thread>

thread_local int g_n = 1;

void f()
{
    g_n++;
    printf("id=%d, n=%d\n", std::this_thread::get_id(),g_n);
}

void foo()
{
    thread_local int i=0;
    printf("id=%d, n=%d\n", std::this_thread::get_id(), i);
    i++;
}

void f2()
{
    foo();
    foo();
}

int main()
{
    g_n++; 
    f();    
    std::thread t1(f);
    std::thread t2(f);
    t1.join();
    t2.join();

    f2();
    std::thread t4(f2);
    std::thread t5(f2);
    t4.join();
    t5.join();
    return 0;
}

程序输出(id值是每次运行时变的,多线程输出结果顺序是随机的,下面是理想的代码顺序输出结果)

g++ -std=c++17 -g -lpthread -fPIC -Wall main.cpp && ./a.out
id=8004, n=3  // 主线程,构建了g_n的一个副本,初值=1,两次自增=3

id=8008, n=2  // 线程t1,构建g_n的另一个副本,初值=1,一次自增=2
id=8012, n=2  // 线程t2,构建g_n的另一个副本,初值=1,一次自增=2

id=8004, n=0  // 主线程,构建i的一个副本,初值=0,i为thread_local的本地变量
id=8004, n=1  // 主进程,f2中i被没有被回收,继续被使用,与被static修饰的静态本地变量类似

id=8016, n=0  // t4进程,构建i的另一个副本,初值=0
id=8016, n=1  // t4进程,继续使用i,与静态本地变量相比,thread_local修饰的本地变量每个线程具有一个副本

id=8020, n=0  // t5进程,与t4结果一致
id=8020, n=1

猜你喜欢

转载自blog.csdn.net/qq_31904421/article/details/107569086
今日推荐