問題描述
使用 Visual Studio 2015,在一個新的空 C++ 項目中,為控制臺應用程序構建以下內容:
With Visual Studio 2015, in a new, empty C++ project, build the following for Console application:
int main() {
return 0;
}
在返回時設置斷點并在調試器中啟動程序.在 Windows 7 上,截至斷點,該程序只有一個線程.但是在 Windows 10 上,它有五個(!)線程:主線程和四個等待同步對象的工作線程".
Set a break point on the return and launch the program in the debugger. On Windows 7, as of the break point, this program has only one thread. But on Windows 10, it has five(!) threads: the main thread and four "worker threads" waiting on a synchronization object.
誰在啟動線程池(或者我如何知道)?
Who's starting up the thread pool (or how do I find out)?
推薦答案
Crystal ball 說 Debug > Windows > Threads 窗口在 ntdll.dll!TppWorkerThread
顯示這些線程.一定要讓 Microsoft 符號服務器自己查看,使用工具 > 選項 > 調試 > 符號.
Crystal ball says that the Debug > Windows > Threads window shows these threads at ntdll.dll!TppWorkerThread
. Be sure to enable the Microsoft Symbol Server to see this yourself, use Tools > Options > Debugging > Symbols.
這也發生在 VS2013 中,所以它絕對不是由新的 VS2015 診斷功能引起的,@Adam 的猜測是不正確的.
This also happens in VS2013 so it is most definitely not caused by the new VS2015 diagnostic features, @Adam's guess cannot be correct.
TppWorkerThread() 是線程池線程的入口點.當我在此函數上使用 Debug > New Breakpoint > Function Breakpoint 設置斷點時.當第二個線程池線程開始執行時,我很幸運地為第一個線程池線程捕獲了這個堆棧跟蹤:
TppWorkerThread() is the entrypoint for a thread-pool thread. When I set a breakpoint with Debug > New Breakpoint > Function Breakpoint on this function. I got lucky to capture this stack trace for the 1st threadpool thread when the 2nd threadpool thread started executing:
ntdll.dll!_NtOpenFile@24() Unknown
ntdll.dll!LdrpMapDllNtFileName() Unknown
ntdll.dll!LdrpMapDllSearchPath() Unknown
ntdll.dll!LdrpProcessWork() Unknown
ntdll.dll!_LdrpWorkCallback@12() Unknown
ntdll.dll!TppWorkpExecuteCallback() Unknown
ntdll.dll!TppWorkerThread() Unknown
kernel32.dll!@BaseThreadInitThunk@12() Unknown
ntdll.dll!__RtlUserThreadStart() Unknown
> ntdll.dll!__RtlUserThreadStart@8() Unknown
顯然加載器正在使用 Windows 10 上的線程池來加載 DLL.這當然是新的:) 此時主線程也在加載器中執行,并發在工作.
Clearly the loader is using the threadpool on Windows 10 to load DLLs. That's certainly new :) At this point the main thread is also executing in the loader, concurrency at work.
因此,Windows 10 正在利用多個內核來更快地初始化進程.非常重要的功能,而不是錯誤:)
So Windows 10 is taking advantage of multiple cores to get the process initialized faster. Very much a feature, not a bug :)
這篇關于為什么 Windows 10 會在我的程序中啟動額外的線程?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!