Multithreading 来自非Qt线程的QThread::getCurrentThread()
如果从Multithreading 来自非Qt线程的QThread::getCurrentThread(),multithreading,qt,Multithreading,Qt,如果从非Qt线程调用它,我将从QThread::getCurrentThread()获得什么 谢谢 QThread只是一个包装器,在幕后它使用本机线程 QThread::currentThread创建并初始化Q(已采用的)线程实例(如果该实例尚不存在) 对于unix,它使用pthreads #include <iostream> #include <thread> #include <pthread.h> #include <QThread> #
非Qt线程调用它,我将从QThread::getCurrentThread()
获得什么
谢谢 QThread
只是一个包装器,在幕后它使用本机线程
QThread::currentThread
创建并初始化Q(已采用的)线程
实例(如果该实例尚不存在)
对于unix,它使用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();
}
我看到这是Qt应用程序主线程的初始化:
data->threadId = (Qt::HANDLE)pthread_self();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;
所以可能会有一些副作用
我建议不要将QThread与非Qt线程混合使用。为什么不试试看呢?你是说QThread::currentThread()?还有一个问题:你为什么要这样做?这可能是未定义的行为。可能会有什么副作用?你为什么不建议把它们混在一起呢?另一种方法是兼容的吗?i、 e.使用
QThread
s创建一个线程,然后使用this\u thread::get\u id()
获取id。
data->threadId = (Qt::HANDLE)pthread_self();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;