From 3ec57107cedb154f256e3ad001ea5475cc64fa94 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 2 May 2016 22:01:38 -0700 Subject: Windows: stop using _beginthreadex on regular builds This commit also reverts fecaa6aae83a3ffa8f1fd41c5aa8275a1bfa7c9b. The Microsoft documentation says _beginthreadex and _endthreadex are used to initialize the C/C++ runtime and are necessary when linking to libcmt(d).lib (that is, when using the -MT or -MTd options). For regular builds linking against the .dll runtime, there should be no impact. Inspection of the CRT source code which gets installed with Visual Studio or Windows SDK proves that. It's preferable to use CreateThread directly as _endthreadex will try to call FreeLibraryAndExitThread, which can cause a deadlock if we try to wait for the thread to exit from a global destructor. For -MT builds, since there can be no DLLs, it's not a problem to continue to use _beginthreadex and follow Microsoft's recommendation. Task-number: QTBUG-53031 Change-Id: Id5480807d25e49e78b79ffff144af62c3c59dfe0 Reviewed-by: Maurice Kalinowski Reviewed-by: Friedemann Kleint --- src/corelib/thread/qthread_win.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/corelib/thread/qthread_win.cpp') diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index 74e191f889..a14c193bad 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -482,7 +482,6 @@ void QThread::start(Priority priority) d->returnCode = 0; d->interruptionRequested = false; -#ifndef Q_OS_WINRT /* NOTE: we create the thread in the suspended state, set the priority and then resume the thread. @@ -493,9 +492,21 @@ void QThread::start(Priority priority) less than NormalPriority), but the newly created thread preempts its 'parent' and runs at normal priority. */ +#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 // !Q_OS_WINRT +#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(&d->id)); #endif // Q_OS_WINRT -- cgit v1.2.3