为什么 Windows 10 会在我的程序中启动额外的线程?

Why does Windows 10 start extra threads in my program?(为什么 Windows 10 会在我的程序中启动额外的线程?)
本文介绍了为什么 Windows 10 会在我的程序中启动额外的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

使用 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 会在我的程序中启动额外的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Returning a pointer of a local variable C++(返回局部变量 C++ 的指针)
Inline function linkage(内联函数联动)
Which is more efficient: Return a value vs. Pass by reference?(哪个更有效:返回值与通过引用传递?)
Why is std::function not equality comparable?(为什么 std::function 不具有可比性?)
C++ overload resolution(C++ 重载解析)
When to Overload the Comma Operator?(什么时候重载逗号运算符?)