在多线程VS代码中获取调试错误,无法找到问题

时间:2013-11-18 01:02:19

标签: c++ multithreading visual-studio debugging c++11

我一直收到调试错误,告诉我abort()已被调用,然后当我在Visual Studio中进行调试时,它会转到下面的代码(最后一行是它抛出的地方):< / p>

void __cdecl _NMSG_WRITE (
        int rterrnum
        )
{
        const wchar_t * const error_text = _GET_RTERRMSG(rterrnum);

        if (error_text)
        {
            int msgshown = 0;
#ifdef _DEBUG
            /*
             * Report error.
             *
             * If _CRT_ERROR has _CRTDBG_REPORT_WNDW on, and user chooses
             * "Retry", call the debugger.
             *
             * Otherwise, continue execution.
             *
             */

            if (rterrnum != _RT_CRNL && rterrnum != _RT_BANNER && rterrnum != _RT_CRT_NOTINIT)
            {
                switch (_CrtDbgReportW(_CRT_ERROR, NULL, 0, NULL, L"%s", error_text))
                {
                case 1: _CrtDbgBreak(); msgshown = 1; break;

当我在下面的函数中跳过最后一行时出现:

X::my_func(){

    //a,b,c are 2x ints and a unordered_map
    std::thread t1(&X::multi_thread_func, this, a, b, c);

    int c = 0;

    //The debug error message doesn't appear until I step over this line. If I were
    //to add further code to this function then the error only appears after stepping
    //over the last line in the function.
    int c1 = 0;
}

我很欣赏这里没有太多东西 - 但人们可以向我提供如何在Visual Studio 2012中继续调查的提示吗?

编辑:如果我删除多线程调用我没有收到错误

1 个答案:

答案 0 :(得分:1)

std::thread实例必须加入或分离,然后才能被破坏(只要t1超出范围,就会发生什么)。否则,std::thread的析构函数将调用std::terminate()。这可能是导致中止的原因。

X::my_func(){

    //a,b,c are 2x ints and a unordered_map
    std::thread t1(&X::multi_thread_func, this, a, b, c);
    t1.join();// <--- ... however, using a thread like this makes little to no sense.

    int c = 0;

    //The debug error message doesn't appear until I step over this line. If I were
    //to add further code to this function then the error only appears after stepping
    //over the last line in the function.
    int c1 = 0;
}