如何正确地终止一个挂线在dll内? [英] How to terminate a hanging thread inside a dll correctly?

查看:149
本文介绍了如何正确地终止一个挂线在dll内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个包含错误的第三方库。当我调用一个函数时,它可能会挂起。库函数在dll中调用。我决定把调用移入线程,等待一段时间。如果线程完成,然后确定。



简单示例如下:

  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进程超时;
}
else
{
wcout<< L过程已结束确定;
}

CloseHandle(hThread);

wcout<<计数器;
}

unsigned int _stdcall DoSomething(void * / * dummy * /)
{
while(1)
{

++ Counter;

}
_endthreadex(0);
return 0;
}

问题


  1. 不建议使用TerminateThread()函式。

  2. 如前所述,线程正在运行里面的dll 。如果我终止线程使用 TerminateThread()我的dll不会卸载使用 FreeLibrary()或甚至 FreeLibraryAndExitThread()

如何终止主题并保留FreeLibrary()不幸的是,你不能安全地任意终止线程。



TerminateThread会导致线程立即终止,即使线程持有锁或修改某些内部状态。 TerminateThread可以导致应用程序中的随机挂起(如果线程持有锁)或崩溃(如果线程正在修改某些状态,并且不一致)



你不能相信DLL行为正确,这会导致严重的可靠性问题,你应该将调用DLL的代码移动到一个单独的进程 - 终止一个进程是更安全。


Hi Everybody,

I have a third party library that contains an error. When I call a function it may hang. The library function is called inside a dll. I decided to move the call into the thread and wait for some time. If thread is finished then OK. If not – I should terminate it compulsory.

The simplified example here:

unsigned Counter = 0;
void f()
{
    HANDLE hThread;
    unsigned threadID;

    // Create the second thread.
    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 * /*dummy*/ )
{
    while (1)
    {

    	++Counter;

    }
    _endthreadex( 0 );
    return 0;
}

The Question

  1. The TerminateThread() function is not recommended to call.
  2. As I mentioned before, the thread is running inside a dll. If I terminate the thread using TerminateThread() my dll would not unload using FreeLibrary() or even FreeLibraryAndExitThread(). Both functions hangs.

How to Terminate the thread and keep FreeLibrary() working?

Thanks.

解决方案

Unfortunately, you can't arbitrarily terminate a thread safely.

TerminateThread causes the thread to terminate immediately, even if the thread is holding locks or modifying some internal state. TerminateThread can cause random hangs in your application (if the thread was holding a lock) or a crash (if the thread were modifying some state and it is left inconsistent)

If you cannot trust the DLL to behave correctly and this is causing significant reliability issues for you, you should move the code invoking the DLL into a separate process - terminating a process is much safer.

这篇关于如何正确地终止一个挂线在dll内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆