我想使用多线程和封装在函数foo中的所有东西来做一些事情。
filterThread = _beginthread (foo, 0,NULL) ;我想让foo返回值:
int foo()
{
return iRet;
}但是_beginthread _CRTIMP uintptr_t __cdecl _beginthread (_In_ void (__cdecl * _StartAddress) (void *), _In_ unsigned _StackSize, _In_opt_ void * _ArgList)的原型表明foo必须是空的,这意味着不能返回值。有没有其他方法可以让foo返回值?
发布于 2011-09-19 11:12:08
要获取线程的返回值,也就是退出代码:
在线程句柄上调用this函数,
DWORD ExitCode;
GetExitCodeThread(hThread, &ExitCode);例如,可以考虑使用_beginthreadex,
unsigned __stdcall foo( void* pArguments )
{
_endthreadex( 0 );
return 0;
}
int main()
{
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex( NULL, 0, foo, NULL, 0, &threadID );
WaitForSingleObject( hThread, INFINITE );
CloseHandle( hThread );
}发布于 2011-09-19 11:19:37
Use _beginthreadex instead.This允许您使用返回值的函数。然后,您可以在线程完成时使用GetExitCodeThread获取该值。
https://stackoverflow.com/questions/7466025
复制相似问题