Hello everybody,
I have a third-party library containing an error. When I call a function, it may hang. The library function is called inside the dll. I decided to transfer the call to the stream and wait a while. If the stream is completed, then OK. If not, I have to finish it mandatory.
A simplified example here:
unsigned Counter = 0;
void f()
{
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex( NULL, 0, DoSomething, NULL, 0, &threadID );
if (WAIT_TIMEOUT == WaitForSingleObject( hThread, 5000 ))
{
TerminateThread(hThread, 1);
wcout << L"Process is Timed Out";
}
else
{
wcout << L"Process is Ended OK";
}
CloseHandle(hThread);
wcout << Counter;
}
unsigned int _stdcall DoSomething( void * )
{
while (1)
{
++Counter;
}
_endthreadex( 0 );
return 0;
}
Question
- The TerminateThread () function is not recommended to be called.
- As I mentioned earlier, a thread works inside a dll . If I terminate the thread using TerminateThread (), my dll will not unload using FreeLibrary () or even FreeLibraryAndExitThread (). Both functions freeze.
How to terminate a thread and keep FreeLibrary () working?
Thank.