我使用QThread
作为线程的管理器,我想知道是否有可能实例化多个QThread
对象,所有管理同一个线程?
当然不可以。
一个线程可以只由1个QThread管理,因为它将在void QThread::start(Priority)
内部创建,没有办法将线程设置为QThread
From qthread_unix.cpp
int code = pthread_create(&threadId, &attr, QThreadPrivate::start, this);
pthread_create
将启动一个新线程。
From 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
都将创建一个新线程。