c++ - QThread::currentThread () 与 QObject::thread()

标签 c++ multithreading qt qthread qobject

除了第一个函数的常量性之外,如果这两个函数之间有任何区别,我正在寻找答案:

QThread * QObject::thread() const
QThread * QThread::currentThread()

最佳答案

它们完全不同。

QThread * QObject::thread() const 返回特定 QObject 所在的线程。

QThread * QThread::currentThread() 返回指向管理当前执行线程的 QThread 的指针。

class MyClass : public QObject
{

};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    MyClass * obj = new MyClass();
    QThread thread2;
    obj->moveToThread(&thread2);
    thread2.start();

    qDebug() << "The current thread is " << QThread::currentThread();
    qDebug() << "The thread2 address is " << &thread2;
    qDebug() << "The object is in thread " << obj->thread();

    return app.exec();
}

示例输出:

The current thread is QThread(0x1436b20)
The thread2 address is QThread(0x7fff29753a30)
The object is in thread QThread(0x7fff29753a30)

关于c++ - QThread::currentThread () 与 QObject::thread(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33280672/

相关文章:

c++ - Windows 上的堆损坏,但 Linux 上没有

c++ - Visual Studio C++连接MYSQL服务器

java - 套接字编程 - BindException - 地址已被使用

c++ - 从 QGridLayout 中删除小部件

c++ - wxSizer 和 wxStaticBitmap 忽略最小尺寸

c++ - boost 图列表或 vec

java - 尝试一次只运行 5 个线程的 selenium webdriver,重复直到运行 200 个线程

c - 使用管道在线程之间发送数据

c++ - 与 Qt 第三方库的静态链接

c++ - 在 QLabel 中显示图像