简体   繁体   English

在std :: thread或pthread_create中创建的QObject的QThreadData是什么?

[英]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(&current_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: 我的问题是:

  1. Is my understanding correct? 我的理解正确吗?
  2. If yes, does not this cause a QueuedConnection to a slot in this QObject to be called in a completely wrong eventLoop (the event loop which is associated with the last "currentThreadData" set by the QT) 如果是,这是否会导致在完全错误的eventLoop(与QT设置的最后一个“ currentThreadData”关联的事件循环)中调用到此QObject中的插槽的QueuedConnection。
  3. if no, how can QT understand that a Qobject created within pthread_create is not associated with any event loop? 如果不是,QT如何理解在pthread_create中创建的Qobject与任何事件循环都没有关联?

(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.

相关问题 std :: thread是否调用pthread_create - Does std::thread call pthread_create 有没有像pthread_create这样的std :: thread_create? - Is there an std::thread_create like pthread_create? 如何在pthread_create创建的子线程中调用主线程? - How to call main thread in the child thread created by pthread_create? 在工作线程中创建的QObject的线程亲和性会发生什么变化呢? - What happens to the thread affinity of a QObject created within a worker thread which then terminates? 为什么我需要从主线程使用 `pthread_exit()`,而它不是由 `pthread_create` 创建的? - Why do I need to use `pthread_exit()` from the main thread, while it was not created by a `pthread_create`? 当std :: lock_guard仍然在作用域内时,使用pthread_create创建线程是否安全? - Is it safe to create a thread using pthread_create when std::lock_guard is still in scope? 使用ASIO和std :: thread制作C ++ 11应用程序时出现错误的`pthread_create&#39;错误引用 - undefined reference to `pthread_create' Error when making C++11 application with ASIO and std::thread 如果未在pthread_create上将其作为参数传递,是否可以从C / C ++的线程中获取父线程ID? - Is it possible from within a thread in C/C++ to get a parent thread id if it wasn't passed in as an argument on pthread_create? pthread_create没有参数? - pthread_create with no arguments? pthread_create混乱 - pthread_create confusion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM