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



下面的程序在GDB 7.4(MinGW32,Eclipse,Windows)中保持挂起/冻结绝对随机,估计大约每5或6次运行一次。通过捣碎 eclipse 中的调试按钮,然后检查尚未终止的调试实例,最容易找到它。当然,您可以通过像正常人一样运行它来做同样的事情,并且可能很快就会得到相同的结果。

样品在未连接到 GDB 时永远不会冻结。曾。我也无法在VC++ Express下公开相同的问题(这是一个不同的示例,但实际上只是相同的想法)。

它主要围绕线程创建、线程删除和程序终止。同样值得注意的是,即使 main 返回 -1 作为退出代码,当附加到 GDB 时,只要程序不冻结,它就会以代码 0 退出。

另一个有趣的事实 - 当我取消注释"Sleep(1)"调用时,它在 80% 的时间内停止挂起。但是,当它冻结时,它会在打印"Return -1"后冻结。所有其他终止继续返回 0(除非在没有 gdb 的情况下运行)。

事不宜迟,代码:

#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 %in", (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: %in", runCount);
        runCount++;
        if(runCount == 20)
            runMain = false;
    }
    printf("Return -1n");
    return -1;
}

您认为是什么原因造成的,更重要的是 - 我该如何解决它?

没有解决方案。GDB #17247 中存在一个相关错误。

将 SIGCONT 信号发送到 gdb 或您的应用程序可以使其再次工作。

下次尝试运行以下命令:

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

kill -18 <gdb pid|application pid>

最新更新