Home >Backend Development >C++ >Why are Multiple Threads Created in Windows 10 C Applications?
Windows 10's Mysterious Thread Creation in C Applications
In this inquiry, a perplexing observation was made regarding the unexpected creation of additional threads when running a simple C program in Windows 10. Using Visual Studio 2015, a new Console application was created with the following code:
<code class="cpp">int main() { return 0; }</code>
Upon setting a breakpoint at the return statement and running the program in the debugger, the program exhibited only one thread on Windows 7. However, on Windows 10, a surprising number of five threads were present: the main thread and four additional "worker threads" waiting on a synchronization object.
Unveiling the Thread Origin
The question posed was: "Who's starting up these extra threads?" To investigate, it was suggested to access the "Debug > Windows > Threads" window and enable the Microsoft Symbol Server for improved visibility. This revealed that the additional threads were identified as "ntdll.dll!TppWorkerThread."
The Role of DLL Loading
Setting a breakpoint at the TppWorkerThread() entrypoint yielded a valuable stack trace that provided insights into the origin of these threads. It was discovered that the Windows 10 loader was utilizing the thread pool to load DLLs. This behavior was absent in Windows 7, indicating a significant change in Windows 10's initialization process.
Concurrent Initialization for Faster Startup
By leveraging multiple cores, Windows 10 effectively speeds up the initialization of the application by executing both the main thread and the DLL loading tasks concurrently. This behavior enhances the overall initialization performance and showcases the advantages of Windows 10's advanced resource management capabilities.
The above is the detailed content of Why are Multiple Threads Created in Windows 10 C Applications?. For more information, please follow other related articles on the PHP Chinese website!