如何正确终止dll中的挂线? [英] How to terminate a hanging thread inside a dll correctly?

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

问题描述

大家好,



我有一个包含错误的第三方库。当我调用一个函数时,它可能挂起。库函数在dll中调用。我决定把电话转入线程,等待一段时间。如果线程完成,那么OK。如果没有 - 我应该终止它。



这里的简化示例:

  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)
{

++计数器

}
_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天全站免登陆