簡體   English   中英

為什么這個簡單的多線程程序在 eclipse - mingw32/gdb 中凍結?

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

下面的程序在 GDB 7.4(MinGW32、Eclipse、Windows)中絕對隨機地保持掛起/凍結,估計大約每 5 或 6 次運行。 通過在 eclipse 中按下調試按鈕然后檢查尚未終止的調試實例,可以很容易地找到它。 您當然可以通過像正常人一樣運行它來做同樣的事情,並且很可能很快就會得到相同的結果。

樣本在未附加到 GDB 時永遠不會凍結。 曾經。 我也無法在 VC++ Express 下暴露相同的問題(這是一個不同的示例,但實際上只是相同的想法)。

它主要圍繞着線程創建、線程刪除和程序終止。 同樣值得注意的是,即使 main 返回 -1 作為退出代碼,只要程序沒有凍結,它就會以代碼 0 退出時附加到 GDB。

另一個有趣的事實——當我取消對“Sleep(1)”調用的注釋時,它會在 80% 的時間內停止掛起。 但是,當它確實凍結時,它會在打印“Return -1\n”后凍結。 所有其他終止繼續返回 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 %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;
}

您認為是什么原因造成的,更重要的是 - 我該如何解決?

沒有解決辦法。 GDB #17247相關的錯誤。

向 gdb 或您的應用程序發送 SIGCONT 信號可使其再次運行。

下次嘗試運行此命令:

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

要么

kill -18 <gdb pid|application pid>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM