为什么C ++线程类创建两个线程?

时间:2014-02-09 12:23:18

标签: multithreading class visual-c++ c++11

我正在运行Visual C ++ 2013,我注意到使用std :: thread类创建一个线程会产生两个线程。这是设计的吗?如果是这样,原因是什么?

当我使用_beginthreadex()时,它只会产生一个我期望的线程。

unsigned int __stdcall Func(void*)
{
    unsigned int i = 0;
    while (i < 1000000000)
    {
        ++i;
    }
    return i;
}

int wmain()
{
    thread doStuff(Func, nullptr);
    auto id = doStuff.get_id();
    doStuff.join();
}

编辑1

当我在doStuff.join()上放置一个断点时,我看到以下输出。 id变量与55760线程匹配。当我使用_beginthreadex()时,我没有得到额外的线程“ntdll.dll线程”。

编辑2

这是加载了符号的调用堆栈。

ThreadTest.exe!wmain() Line 21
ThreadTest.exe!__tmainCRTStartup() Line 623
ThreadTest.exe!wmainCRTStartup() Line 466
kernel32.dll!@BaseThreadInitThunk@12()
ntdll.dll!___RtlUserThreadStart@8()
ntdll.dll!__RtlUserThreadStart@8()

1 个答案:

答案 0 :(得分:1)

主线是显而易见的。这是你的主线。创建线程时,只会创建一个线程。 msvcr *线程是Microsoft C运行时库。我不认为你可以控制它但不介意。您的代码按预期工作。