Share via


Can an MFC DLL create multiple threads?

OverviewHow Do IFAQDetailsSample

Except during initialization, an MFC DLL can safely create multiple threads as long as it uses the Win32 thread local storage (TLS) functions such as TlsAlloc to allocate thread local storage. However, if an MFC DLL uses __declspec(thread) to allocate thread local storage, the client application must be implicitly linked to the DLL. If the client application explicitly links to the DLL, the call to LoadLibrary will not successfully load the DLL. For more information on creating multiple threads inside MFC DLLs, see the Knowledge Base article, "PRB: LoadLibrary() Fails with _declspec(thread)" (Q118816).

An MFC DLL that creates a new MFC thread during startup will hang when it is loaded by an application. This includes whenever a thread is created by calling AfxBeginThread or CWinThread::CreateThread inside:

  • The InitInstance of a CWinApp-derived object in a regular DLL.

  • A supplied DllMain or RawDllMain function in a regular DLL.

  • A supplied DllMain or RawDllMain function in an Extension DLL.

For more information about creating threads during initialization, see the Knowledge Base article, "PRB: Cannot Create an MFC Thread During DLL Startup" (Q142243).