QThread::getCurrentThread() from non-Qt thread
multithreading, qt
Solution
`QThread` is just a wrapper, behind the scene it uses native threads.
`QThread::currentThread` creates and initialises a `Q(Adopted)Thread` instance if it doesn't exist yet.
In 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();
}
Output:
QThread(0x7fce51410fd0)
0x10edb6000
0x10edb6000
0x10edb6000
finished
I see that there is initialisation of Qt application main thread:
data->threadId = (Qt::HANDLE)pthread_self();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;
So there might be some side effects.
I'd advise not to mix QThread with non-Qt threads.
Problem
What will I get from `QThread::getCurrentThread()`, if it is called from `non-Qt thread`? Thanks!