Qt:用 __thread 关键字让每个线程有自己的全局变量
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wsj18808050/article/details/51603006
在GUN标准中,提供了__thread关键字,配合static后,可以实现让一个线程拥有自己的全局变量。
我对__thread进行了简单的封装,可以用于存储class。并且防止了内存泄露(如果使用Qt线程类)。
测试中,我一共开启了两个线程,从输出可以得知每个线程都拥有自己的变量,并且在线程退出后被正常释放。
测试代码:
// Qt lib import
#include <QtCore>
#include <QtConcurrent>
class MyClass
{
public:
MyClass()
{
qDebug() << "MyClass" << this << QThread::currentThread();
}
~MyClass()
{
qDebug() << "~MyClass" << this << QThread::currentThread();
}
};
template< class T >
class ThreadStore
{
public:
T &getQuote()
{
if ( !t )
{
t = new T;
auto thread = qobject_cast< QThread * >( QThread::currentThread() );
if ( thread && thread->isRunning() )
{
QObject::connect( QThread::currentThread(), &QThread::finished, [ this ]()
{
if ( t )
{
delete t;
t = nullptr;
}
} );
}
}
return *t;
}
private:
T *t;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto myTest = []()
{
static __thread ThreadStore< MyClass > threadStore;
auto &myData = threadStore.getQuote();
qDebug() << "myData:" << &myData;
QThread::sleep( 1 );
};
QThreadPool::globalInstance()->setMaxThreadCount( 2 );
QtConcurrent::run( myTest );
QtConcurrent::run( myTest );
QtConcurrent::run( myTest );
QtConcurrent::run( myTest );
QThreadPool::globalInstance()->waitForDone();
return a.exec();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
输出结果:
MyClass 0x7fe842e09b10 QThread(0x7fe842d0cbe0, name = "Thread (pooled)")
MyClass 0x7fe842c1d6a0 QThread(0x7fe842d0df60, name = "Thread (pooled)")
myData: 0x7fe842e09b10
myData: 0x7fe842c1d6a0
myData: 0x7fe842e09b10
myData: 0x7fe842c1d6a0
~MyClass 0x7fe842e09b10 QThread(0x7fe842d0cbe0, name = "Thread (pooled)")
~MyClass 0x7fe842c1d6a0 QThread(0x7fe842d0df60, name = "Thread (pooled)")
————————————————
版权声明:本文为CSDN博主「Jason188080501」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wsj18808050/article/details/51603006
Qt:用 __thread 关键字让每个线程有自己的全局变量的更多相关文章
- gcc 中__thread 关键字的示例代码
__thread 关键字的解释: Thread Local Storage 线程局部存储(tls)是一种机制,通过这一机制分配的变量,每个当前线程有一个该变量的实例. gcc用于实现tls的运行 ...
- __thread关键字[转]
自 http://blog.csdn.net/liuxuejiang158blog/article/details/14100897 __thread是GCC内置的线程局部存储设施,存取效率可以和全局 ...
- Qt之可重入与线程安全
简述 本篇文章中,术语"可重入性"和"线程安全"被用来标记类与函数,以表明它们如何被应用在多线程应用程序中. 一个线程安全的函数可以同时被多个线程调用,甚至调用 ...
- [转] __thread关键字
http://blog.csdn.net/liuxuejiang158blog/article/details/14100897 __thread是GCC内置的线程局部存储设施,存取效率可以和全局变量 ...
- 多线程编程--- __thread关键字
__thread是GCC内置的线程局部存储设施,存取效率可以和全局变量相比.__thread变量每一个线程有一份独立实体,各个线程的值互不干扰.可以用来修饰那些带有全局性且值可能变,但是又不值得用全局 ...
- 【Qt开发】事件循环与线程 二
事件循环与线程 二 Qt 线程类 Qt对线程的支持已经有很多年了(发布于2000年九月22日的Qt2.2引入了QThread类),Qt 4.0版本的release则对其所有所支持平台默认地是对多线程支 ...
- 【Qt开发】事件循环与线程 一
事件循环与线程 一 初次读到这篇文章,译者感觉如沐春风,深刻体会到原文作者是花了很大功夫来写这篇文章的,文章深入浅出,相信仔细读完原文或下面译文的读者一定会有收获. 由于原文很长,原文作者的行文思路是 ...
- Qt多线程编程中的对象线程与函数执行线程
近来用Qt编写一段多线程的TcpSocket通信程序,被其中Qt中报的几个warning搞晕了,一会儿是说“Cannot create children for a parent that is in ...
- QT信号和槽在哪个线程执行问题
时隔四个月后的第一篇,换了个公司可以登录的博客,记录一些学习内容吧 这是看到别人写的比较好的一篇,排版有点乱 QThread的使用方法 起源 昨天不小心看到Qt开发人员( Bradley T.Hugh ...
随机推荐
- springIoC注解
一.通过注解注入Bean @Component:组件(spring扫描包下有该注解的类) @ComponentScan(包名):组件扫描(spring扫描该包名下的类) @Configuration: ...
- Hiberfil.sys
Hiberfil.sys 命令窗口中输入 powercfg -h off,即可关闭休眠功能,同时 Hiberfil.sys 文件也会自动删除.
- 对pdf中的图片进行自动识别
对pdf中的图片进行自动识别 商务合作,科技咨询,版权转让:向日葵,135—4855__4328,xiexiaokui#qq.com 原理:增强扫描 效果:自动识别所有图片中的文字,可以选择.复制,进 ...
- Redis数据库漏洞防护
Redis是一个高性能的数据库,Redis Crackit及Redis安全漏洞本质上是由于Redis自身缺乏安全防护机制,同时Redis的使用者又未曾遵循官方的安全规范所导致的. Redis安全漏洞 ...
- 006 GET API
1.说明 The get API allows to get a JSON document from the index based on its id. GET通过基于id的索引获取JSON文档. ...
- flutter Card卡片列表组件
一个 Material Design 卡片.拥有一个圆角和阴影 import 'package:flutter/material.dart'; import './model/post.dart'; ...
- 1. Tomcat之startup.sh
#判断操作系统os400=falsecase "`uname`" inOS400*) os400=true;;esac # 取脚本名称PRG="$0" # 判断 ...
- 004-行为型-01-策略模式(Strategy)
一.概述 定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户.需要设计一个接口,为一系列实现类提供统一的方法,多个实现类实现该接口,设计一个抽象类(可有可 ...
- 002-06-RestTemplate-请求示例-form、json、multipart、okhttp3
一.概述 请求示例集合 服务端:https://github.com/bjlhx15/common-study.git 中的 http-client-webserver 服务端:RequestBody ...
- [译]使用Pandas读取大型Excel文件
上周我参加了dataisbeautiful subreddit上的Dataviz Battle,我们不得不从TSA声明数据集创建可视化.我喜欢这种比赛,因为大多数时候你最终都会学习很多有用的东西. 这 ...