繁体   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