gpt4 book ai didi

c - WaitForSingleObject 无限等待线程终止

转载 作者:行者123 更新时间:2023-11-30 19:19:10 25 4
gpt4 key购买 nike

我有两个功能:

Fl_Thread n_create_thread(Fl_Thread &t, void *(*f) (void *), void *p) 
{
return t = (Fl_Thread)_beginthread((void(__cdecl *)(void *))f, 0, p);
}

void n_wait_end_thread(Fl_Thread t)
{
WaitForSingleObject((HANDLE)t, INFINITE);
}

创建线程,给出指向线程函数的指针,但是如果我等待通过WaitForSingleObject终止线程,它会挂起并且无法停止等待。

最佳答案

当您等待时,您的线程句柄可能无效 - 请改用 _beginthreadex - see the MSDN page :

It's safer to use _beginthreadex than _beginthread. If the thread that's generated by _beginthread exits quickly, the handle that's returned to the caller of _beginthread might be invalid or point to another thread. However, the handle that's returned by _beginthreadex has to be closed by the caller of _beginthreadex, so it is guaranteed to be a valid handle if _beginthreadex did not return an error.

关于c - WaitForSingleObject 无限等待线程终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25314759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com