简体   繁体   English

为什么这个简单的多线程程序在 eclipse - mingw32/gdb 中冻结?

[英]Why is this simple multithreaded program freezing in eclipse - mingw32/gdb?

The program below keeps hanging/freezing in GDB 7.4 (MinGW32, Eclipse, Windows) absolutely randomly, estimating about every 5 or 6 runs.下面的程序在 GDB 7.4(MinGW32、Eclipse、Windows)中绝对随机地保持挂起/冻结,估计大约每 5 或 6 次运行。 It is most easily found by mashing the debug button in eclipse and then checking the debug instances that haven't terminated.通过在 eclipse 中按下调试按钮然后检查尚未终止的调试实例,可以很容易地找到它。 You can of course do the same thing by running it like a normal person and likely will get the same result quite soon.您当然可以通过像正常人一样运行它来做同样的事情,并且很可能很快就会得到相同的结果。

The sample never freezes when not attached to GDB.样本在未附加到 GDB 时永远不会冻结。 Ever.曾经。 I also could not expose the same issue under VC++ Express (it was a different sample but really just the same idea).我也无法在 VC++ Express 下暴露相同的问题(这是一个不同的示例,但实际上只是相同的想法)。

It hangs mostly around thread creation, thread deletion, and program termination.它主要围绕着线程创建、线程删除和程序终止。 Also worth of note is that even though main returns -1 as exit code, when attached to GDB whenever the program doesn't freeze it exits with code 0.同样值得注意的是,即使 main 返回 -1 作为退出代码,只要程序没有冻结,它就会以代码 0 退出时附加到 GDB。

Another interesting fact - when I uncomment the "Sleep(1)" call it stops hanging 80% of the time.另一个有趣的事实——当我取消对“Sleep(1)”调用的注释时,它会在 80% 的时间内停止挂起。 When it does freeze however, it freezes after it prints "Return -1\n".但是,当它确实冻结时,它会在打印“Return -1\n”后冻结。 All other terminations continue to return 0 (except when ran w/o gdb).所有其他终止继续返回 0(不使用 gdb 运行时除外)。

Without further ado, the code:废话不多说,上代码:

#include <stdio.h>
#include <windows.h>
#include <process.h>

void __cdecl callback(void *arg)
{
    int count = 0;

    while(count < 10)
    {
        printf("Thread 2(%i): looping %i\n", (int)arg, count);
        count++;
    }

    printf("Ending thread...\n");
    _endthread();
}

int main(int argc, char *argv[])
{
    // Mingw32 on windows w/ eclipse - fix console output not showing up until the app terminates
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);

    bool runMain = true;

    int runCount = 0;

    while(runMain == true)
    {
        if(runCount == 5)
        {
            printf("Thread starting... ");
            int result = _beginthread(callback, 0, (void*)5);
//          Sleep(1);

            if(result == 0)
                printf("[FAILURE]\n");
            else
                printf("[SUCCESS]\n");
        }

        printf("Thread 1: %i\n", runCount);
        runCount++;

        if(runCount == 20)
            runMain = false;
    }

    printf("Return -1\n");
    return -1;
}

What do you think is causing this, and more importantly - how do I fix it?您认为是什么原因造成的,更重要的是 - 我该如何解决?

There is no solution.没有解决办法。 There was a bug in GDB #17247 related. GDB #17247相关的错误。

Sending a SIGCONT signal to gdb or to your application makes it work again.向 gdb 或您的应用程序发送 SIGCONT 信号可使其再次运行。

Try run this command next time:下次尝试运行此命令:

killall -s SIGCONT <gdb | your application process name>

or要么

kill -18 <gdb pid|application pid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM