[英]What is the QThreadData of a QObject which is created within std::thread or pthread_create
Whenever a QObject is created, QT assigns a QThreadData to the QObject in its constructor. 每当创建QObject时,QT都会在其构造函数中将QThreadData分配给QObject。 Among other things, this QThreadData is then used to check the existence of a event loop associated with the QObject.
除其他外,此QThreadData然后用于检查与QObject相关联的事件循环的存在。
When Qt threading facilities are used (for instance QThread or implicitly via QCoreApplication in single threaded application), Qt assigns meaningful values to QThreadData and everything works as expected. 当使用Qt线程工具(例如QThread或通过单线程应用程序中的QCoreApplication隐式)时,Qt会为QThreadData分配有意义的值,并且一切都会按预期进行。
However, the following source code implies that a QObject created within a manual system interface threading facility (for instance in a pthread_create or std::thread), will simply copy the last QThreadData from the last QT threading facility call: (The code is copied from /qt5/qtbase/src/corelib/thread/qthread_unix.cpp and HAVE_TLS is defined) 但是,以下源代码暗示在手动系统接口线程工具中创建的QObject(例如在pthread_create或std :: thread中)将仅从上次QT线程工具调用中复制最后的QThreadData :(将代码复制从/qt5/qtbase/src/corelib/thread/qthread_unix.cpp中定义了HAVE_TLS)
// Utility functions for getting, setting and clearing thread specific data.
static QThreadData *get_thread_data()
{
#ifdef HAVE_TLS
return currentThreadData;
#else
pthread_once(¤t_thread_data_once, create_current_thread_data_key);
return reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key));
#endif
}
In the above code sample, "currentThreadData" is a static variable which is previously set by the QT during any QT threading facility call (eg QThread). 在上面的代码示例中,“ currentThreadData”是静态变量,该静态变量是在任何QT线程工具调用(例如QThread)期间由QT预先设置的。
My questions are: 我的问题是:
(Please note that I always assume that parent of a QObject is set to NULL. Because when parent is set for a QObject, QThreadData is directly copied from that parent and everything again works correctly.) (请注意,我始终假设QObject的父对象设置为NULL。因为当为QObject设置父对象时,会直接从该父对象复制QThreadData,并且一切都会再次正常进行。)
I'm not sure I entirely understand the question. 我不确定我是否完全理解这个问题。 However, when you say...
但是,当你说...
"currentThreadData" is a static variable which is previously set by the QT during any QT threading facility call (eg QThread).
“ currentThreadData”是静态变量,该静态变量由QT在任何QT线程工具调用(例如QThread)期间预先设置。
...you're missing one important point: currentThreadData
is also declared thread local
. ...您遗漏了一个要点:
currentThreadData
也被声明为thread local
。
In the code base I'm looking at ( Qt 5.13.0 ) it is... 在我正在查看的代码库中( Qt 5.13.0 )它是...
static __thread QThreadData *currentThreadData = 0;
Where __thread
is a compiler specific extension which essentially provides the same thread local storage duration as the c++11
keyword thread_local
. 其中
__thread
是编译器特定的扩展,它实际上提供与c++11
关键字thread_local
相同的线程本地存储持续时间。
Hence, the currentThreadData
variable as seen by one thread is completely separate from the currentThreadData
variable seen by any other thread. 因此,一个线程看到的
currentThreadData
变量与其他任何线程看到的currentThreadData
变量完全分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.