首先,介绍下QThread的currentThreadId()方法:
Qt::HANDLE QThread::currentThreadId () [static]
Returns the thread handle of the currently executing thread.
Warning: The handle returned by this function is used for internal purposes and should not be used in any application code.
Warning: On Windows, the returned value is a pseudo-handle for the current thread. It can't be used for numerical comparison. i.e., this function returns the DWORD (Windows-Thread ID) returned by the Win32 function getCurrentThreadId(), not the HANDLE (Windows-Thread HANDLE) returned by the Win32 function getCurrentThread().
他是QThread类的一个静态方法,返回值是当前thread的HANDLE。
但是,我最近在用他来测试的 时候,发现currentThreadId()和currentThreadId的结果是不一样的,刚开始是忘记加括号,感觉结果不对,最后,找出原因,结果就正确了。
下面是我的代码:
//test.h文件
#include <QtGui/QMainWindow>
#include <QThread>
calss Test : public QWidget
{
Q_OBJECT
public:
test();
~test();
};
class MyThread : public QThread
{
public:
MyThread();
~MyThread();
protected:
void run();
};
//test.cpp
#include <test.h>
Test::test() : QWidget
{
MyThread *mThread = new MyThread();
mThread->start();
qDebug()<<"[run中带() = ]"<<QThread::currentThreadId();
qDebug()<<"[run中不带() = ]"<<QThread::currentThreadId;
}
MyThread::mythread()
{
}
void MyThread::run()
{
qDebug()<<"[run中带() = ]"<<currentThreadId();
qDebug()<<"[run中不带() = ]"<<currentThreadId;
}
//main.cpp
#include <test.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
sbpNatureTest w;
w.show();
qDebug()<<"{main中带() = }"<<(int)QThread::currentThreadId();
qDebug()<<"{main中不带() = }"<<(int)QThread::currentThreadId;
return a.exec();
}
上述结果中,除了run()中为次线程,其他地方都为主线程,即,main中和test() 中结果是一致的, run()中和他们不同。
但是,带()和不带()的结果不同。