簡體   English   中英

創建子線程后如何使主線程繼續?

[英]How to make main thread continue after it create child thread?

我正在嘗試制作一個簡單的套接字程序。 當我的服務器發送命令時,客戶端將創建子線程並執行 function。 它等待子線程完成。 但我需要主線程仍然接收我的命令來做另一項工作。 這是我的代碼:

客戶

int StartServices() {   
    HANDLE hThread;     
    DWORD dwThread;     
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TestFunction, NULL, 0, NULL);   
    if (hThread) {      
        return WaitForSingleObject(hThread, 10000);     
    }   
    else {      
        return 1;   
    }
}

如果我做對了,您可能只需要刪除return WaitForSingleObject(hThread, 10000); 從代碼。

int StartServices() {   
    HANDLE hThread;     
    DWORD dwThread;     
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TestFunction, NULL, 0, NULL);   
    if (hThread) {      
        //return WaitForSingleObject(hThread, 10000);  
        return 0;   
    }   
    else {      
        return 1;   
    }
}

因此,不會有 10 秒的等待時間。

暫無
暫無

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

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