2

I wanted to do something using multi-threading and all the stuff encapsulated in function foo .

filterThread = _beginthread (foo, 0,NULL) ;

and I wanted to let foo return value:

int foo()
{
    return iRet;
}

but the prototype of _beginthread _CRTIMP uintptr_t __cdecl _beginthread (_In_ void (__cdecl * _StartAddress) (void *), _In_ unsigned _StackSize, _In_opt_ void * _ArgList) shows that foo must be void which means cannot return value . Is there any way else i can do to let foo return value?

fr33m4n
  • 542
  • 2
  • 13
  • 31

2 Answers2

2

Use _beginthreadex instead.This allows you to use a function that returns a value. You can then use GetExitCodeThread to get the value when the thread completes.

Logan Capaldo
  • 39,555
  • 5
  • 63
  • 78
1

To get the return value aka exit code of thread:

Call this function on thread's handle after it has finished,

DWORD ExitCode;
GetExitCodeThread(hThread, &ExitCode);

As an example, consider using _beginthreadex instead,

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 );
}
cpx
  • 17,009
  • 20
  • 87
  • 142