thread_local变量
thread_local变量是C++ 11新引入的一种存储类型。它会影响变量的存储周期(Storage duration),C++中有4种存储周期:
- automatic
- static
- dynamic
- thread
有且只有thread_local关键字修饰的变量具有线程周期(thread duration),这些变量(或者说对象)在线程开始的时候被生成(allocated),在线程结束的时候被销毁(deallocated)。并且每 一个线程都拥有一个独立的变量实例(Each thread has its own instance of the object)。thread_local
可以和static
与 extern
关键字联合使用,这将影响变量的链接属性(to adjust linkage)。
那么,哪些变量可以被声明为thread_local?以下3类都是ok的
- 命名空间下的全局变量
- 类的static成员变量
- 本地变量
下面引用《C++ Concurrency in Action》书中的例子来说明这3种情况:
thread_local int x; //A thread-local variable at namespace scope
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
}
既然每个线程都拥有一份独立的thread_local变量,那么就有2个问题需要考虑:
- 各线程的thread_local变量是如何初始化的
- 各线程的thread_local变量在初始化之后拥有怎样的生命周期,特别是被声明为thread_local的本地变量(local variables)
下面的代码可以帮助回答这2个问题,我的测试环境是vs2015。
输出的前3行打印能帮助解答thread_local变量是如何初始化的,可以看到每个线程都会进行一次初始化,例子中的g_n在主线程中最早被初始化为1,随后被修改为2和3,但这些修改操作并不影响g_n在线程t2和t3中的初始值(值为1),虽然t2和t3线程启动的时候主线程中的变量值已经被更新为3,所以主线程、thread1、thread2打印结果分别为3,2,2。
后6行打印说明了一个事实,声明为thread_local的本地变量在线程中是持续存在的,不同于普通临时变量的生命周期,它具有static变量一样的初始化特征和生命周期,虽然它并没有被声明为static。例子中foo函数中的thread_local变量 i 在每个线程第一次执行到的时候初始化,在每个线程各自累加,在线程结束时释放。
#include <thread> thread_local int g_n = ; void f()
{
g_n++;
printf("id=%d, n=%d\n", std::this_thread::get_id(),g_n);
} void foo()
{
thread_local int i=;
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 ;
}
输出(id值是每次运行时变的):
id=8004, n=3
id=8008, n=2
id=8012, n=2
id=8004, n=0
id=8004, n=1
id=8016, n=0
id=8016, n=1
id=8020, n=0
id=8020, n=1
thread_local变量的更多相关文章
- thread_local变量(转)
转自:https://www.cnblogs.com/pop-lar/p/5123014.html thread_local变量是C++11新引入的一种存储类型.它会影响变量的存储周期(Storage ...
- c/c++ 多线程 thread_local 类型
多线程 thread_local 类型 thread_local变量是C++ 11新引入的一种存储类型. thread_local关键字修饰的变量具有线程周期(thread duration), 这些 ...
- c++11: thread_local
thread_local变量是C++ 11新引入的一种存储类型.它会影响变量的存储周期(Storage duration),C++中有4种存储周期: automatic static dynamic ...
- [c++] Pieces of knowledge
再系统地学一遍c++,查缺补漏. wchar_t L'a' stored in wchar_t. size of types cout << numeric_limits<doubl ...
- C++14介绍
C++14标准是 ISO/IEC 14882:2014 Information technology -- Programming languages -- C++ 的简称[1] .在标准正式通过之 ...
- C++11 (多线程)并发编程总结
| 线程 std::thread 创建std::thread,一般会绑定一个底层的线程.若该thread还绑定好函数对象,则即刻将该函数运行于thread的底层线程. 线程相关的很多默认是move语义 ...
- Linux 调试: systemtap
安装与配置 在ubuntu下直接用apt-get install之后不能正常使用,提示缺少调试信息或者编译探测代码时有问题. 1. 采用官网上的解决方法 2. 可以自己重新编译一次内核,然后再手工编译 ...
- C++11并发编程个人小结
thread_local变量在每个线程第一次执行到时初始化(类似static),并在每个线程各自累加,并在线程结束时释放. std::condition_variable:: wait(std::un ...
- C++的各种初始化方式
C++小实验测试:下面程序中main函数里a.a和b.b的输出值是多少? #include <iostream> struct foo { foo() = default; int a; ...
随机推荐
- 【C#笔札】1 string类型(2)
4> Trim whitespace Trim 也是string的一个方法节点 C#例子如下: C#中有Trim,TrimEnd 和TrimStart三种Trim节点,其中后两者无需介绍. 如上 ...
- 命令行创建db2数据库
在cmd界面执行db2cmd命令 然后在db2cmd界面执行db2命令 然后执行 CREATE DATABASE UIBS命令 创建完成之后可以使用CONNECT TO UIBS命令连接数据库
- Workflow Builder 2.6.3 Certified on Windows 10 for EBS 12.x
By Steven Chan - EBS-Oracle on May 17, 2016 Workflow Builder 2.6.3 is now certified on Windows 10 de ...
- 【Wannafly挑战赛10 - B】小H和密码(DP)
试题链接:https://www.nowcoder.com/acm/contest/72/B 题目描述 小H在击败怪兽后,被一个密码锁挡住了去路 密码锁由N个转盘组成,编号为1~N,每 ...
- shell编程学习1
1.shell是操作系统的终端命令行 (1)shell可以理解为软件系统提供给用户操作的命令行界面,可以说它是人机交互的一种方式. (2)我们可以使用shell和操作系统.uboot等软件系统进 ...
- 【转载】Git,Github和Gitlab简介和基本使用Gitlab安装和使用
http://blog.csdn.net/u011241606/article/details/51471367
- 原创:项目管理的理论与实践 讲座的PPT
业余时间做的两个PPT,曾经给公司同事讲过,PPT内容毕竟还是不够全面,如果有不清楚的地方,欢迎提问 项目管理的理论与实践 虚拟案例-超市管理系统
- lzugis——Arcgis Server for JavaScript API之POI
POI(Point Of Interest),感兴趣点,其实呢,严格意义上说应该不是POI,但是单位就这样叫了,我也就这样叫了,其实现的功能大致是这样的:用过百度地图的朋友们都知道你在百度地图时,当鼠 ...
- Android 仿淘宝属性标签页
直接看效果图相信这样的效果很多,我之前在网上找了很久没找到自己想要的! <?xml version="1.0" encoding="utf-8"?> ...
- 旧书重温:0day2【8】狙击windows的异常处理实验
现在进入0day2的第六章内容 其中第六章的书本内容我都拍成了图片格式放在了QQ空间中(博客园一张一传,太慢了)http://user.qzone.qq.com/252738331/photo/V10 ...