QThread :: getCurrentThread () from a thread other than Qt

What will I get from QThread::getCurrentThread() if it is called from non-Qt thread ?

Thanks!

+4
source share
1 answer

QThread is just a wrapper, behind the scenes it uses its own threads.

QThread::currentThread creates and initializes an instance of Q(Adopted)Thread , if it does not already exist.

In the case of unix, it uses pthread s.

 #include <iostream> #include <thread> #include <pthread.h> #include <QThread> #include <QDebug> void run() { QThread *thread = QThread::currentThread(); qDebug() << thread; std::cout << QThread::currentThreadId() << std::endl; std::cout << std::this_thread::get_id() << std::endl; std::cout << pthread_self() << std::endl; thread->sleep(1); std::cout << "finished\n"; } int main() { std::thread t1(run); t1.join(); } 

Conclusion:

 QThread(0x7fce51410fd0) 0x10edb6000 0x10edb6000 0x10edb6000 finished 

I see that there is the initialization of the main thread of the Qt application:

 data->threadId = (Qt::HANDLE)pthread_self(); if (!QCoreApplicationPrivate::theMainThread) QCoreApplicationPrivate::theMainThread = data->thread; 

Therefore, there may be some side effects.

I would advise against mixing QThread with threads other than Qt.

+7
source

Source: https://habr.com/ru/post/1211095/