2016-11-17 47 views

回答

1

不,當然。

單個線程只能由1 QThread的管理,因爲它會被裏面void QThread::start(Priority)內部創建,也就是noway設置線程QThread

qthread_unix.cpp

int code = pthread_create(&threadId, &attr, QThreadPrivate::start, this); 

pthread_create將啓動新線程。

qthread_win.cpp

#if defined(Q_CC_MSVC) && !defined(_DLL) // && !defined(Q_OS_WINRT) 
# ifdef Q_OS_WINRT 
    // If you wish to accept the memory leaks, uncomment the part above. 
    // See: 
    // https://support.microsoft.com/en-us/kb/104641 
    // https://msdn.microsoft.com/en-us/library/kdzttdcb.aspx 
# error "Microsoft documentation says this combination leaks memory every time a thread is started. " \ 
    "Please change your build back to -MD/-MDd or, if you understand this issue and want to continue, " \ 
    "edit this source file." 
# endif 
    // MSVC -MT or -MTd build 
    d->handle = (Qt::HANDLE) _beginthreadex(NULL, d->stackSize, QThreadPrivate::start, 
              this, CREATE_SUSPENDED, &(d->id)); 
#else 
    // MSVC -MD or -MDd or MinGW build 
    d->handle = (Qt::HANDLE) CreateThread(NULL, d->stackSize, (LPTHREAD_START_ROUTINE)QThreadPrivate::start, 
              this, CREATE_SUSPENDED, reinterpret_cast<LPDWORD>(&d->id)); 
#endif // Q_OS_WINRT 

兩個CreateThread_beginthreadex將創建當然一個新的線程。

+0

但我已經在我的程序中使用QOject :: QThread()獲得了不同的QThread對象指針,但它們都返回相同的句柄,表明它們實際上在管理相同的線程。這怎麼可能? –

+0

澄清;與句柄我的意思是從QThread返回值:: currenthThreadId() –

+1

@LennartRolland你的意思是['QObject :: thread']函數?該函數返回一個指向該對象所在的'QThread'的指針。由於你的大部分對象都會駐留在主線程中,它將返回**相同的指針**,這意味着它是**相同的'QThread' ** – Danh

相關問題