简体   繁体   English

创建子线程后如何使主线程继续?

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

im trying to make a simple socket program.我正在尝试制作一个简单的套接字程序。 When my server send command, the client will create child thread and execute the function.当我的服务器发送命令时,客户端将创建子线程并执行 function。 It waiting for the child thread to be done.它等待子线程完成。 But i need the main thread still recv my command to do another work.但我需要主线程仍然接收我的命令来做另一项工作。 Here is my code:这是我的代码:

Client客户

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;   
    }
}

If I got it right, You may need just to remove return WaitForSingleObject(hThread, 10000);如果我做对了,您可能只需要删除return WaitForSingleObject(hThread, 10000); from the code.从代码。

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;   
    }
}

So, there will be no 10 secounds waiting.因此,不会有 10 秒的等待时间。

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

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