Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 来自非Qt线程的QThread::getCurrentThread()_Multithreading_Qt - Fatal编程技术网

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;