Ç - 中断它是在一个线程中执行一个函数调用 [英] c - interrupt a function call which is executing in a thread

查看:222
本文介绍了Ç - 中断它是在一个线程中执行一个函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个线程函数的伪code看起来像一个DLL:

 挥发性BOOL停止= FALSE;无效StopEverything()
{
    / *进入临界区* /
    停止= TRUE;
    / *离开临界区* /
}无效workerThreadFunc()
{
    初始化();    / *检查点1 * /
    如果(停止)
    {
        / *做清理* /
        返回;
    }    doLaboriousTask1();    / *检查点2 * /
    如果(停止)
    {
        / *做清理* /
        返回;
    }    doLaboriousTask2();    未初始化();
}

和中,使用此DLL的code,清理功能如下:

 无效清理()
{
    StopEverything();    / *等待所有线程退出* /
    / *做其他清理* /
}

我的问题是双重的:


  1. 有没有更好的办法阻止我的 workerThreadFunc()从执行,而不是在各个检查站的检查做的那样?

  2. 假设 workerThreadFunc()粘贴在 doLaboriousTask2()当主应用程序调用 StopEverything()。有没有一种办法的中断 doLaboriousTask2(),并立即退出?

谢谢!


解决方案

  

有没有更好的办法从执行,而不是在各个检查站的检查做的那样阻止我workerThreadFunc()?


大概不会。有以pre-先发制人阻止非托管code线程没有完全可靠的方法。凡是声称这样做会导致 TerminateThread 。和文档列出种种可怕后果,使用该功能。例如:


  

      
  • 如果目标线程拥有临界区,临界区将不会被释放。

  •   
  • 如果目标线程是从堆中分配内存,堆锁不会被释放。

  •   
  • 如果目标线程执行某些KERNEL32呼叫时终止,KERNEL32的状态的线程的进程可能会
      不一致的。

  •   
  • 如果目标线程操作的共享DLL的全局状态,DLL的状态可能会被破坏,影响其他用户
      该DLL。

  •   

您问:


  

有没有办法打断doLaboriousTask2()并将它立即退出?


好了,你可以调用 TerminateThread 但是,对于文档中描述的所有原因,你几乎肯定不应该这样做。

I have a DLL which contains a thread function whose pseudocode looks like:

volatile BOOL stopped = FALSE;

void StopEverything()
{
    /* Enter critical section */
    stopped = TRUE;
    /* Leave critical section */
}

void workerThreadFunc()
{
    Initialize();

    /* Checkpoint 1 */
    if(stopped)
    {
        /* Do cleanup */
        return;
    }

    doLaboriousTask1();

    /* Checkpoint 2 */
    if(stopped)
    {
        /* Do cleanup */
        return;
    }

    doLaboriousTask2();

    Uninitialize();
}

And in the code which uses this DLL, the cleanup function looks like:

void cleanup()
{
    StopEverything();

    /* Wait for all threads to exit */
    /* Do other cleanup */
}

My question is twofold:

  1. Is there a better way to stop my workerThreadFunc() from executing instead of doing checks like that at various checkpoints?
  2. Suppose workerThreadFunc() is stuck inside doLaboriousTask2() when the main application calls StopEverything(). Is there a way to interrupt doLaboriousTask2() and have it exit immediately?

Thanks!

解决方案

Is there a better way to stop my workerThreadFunc() from executing instead of doing checks like that at various checkpoints?

Probably not. There's no fully reliable way to pre-emptively halt a thread in unmanaged code. Anything that purports to do that leads to TerminateThread. And the documentation lists all sorts of dire consequences for using that function. For example:

  • If the target thread owns a critical section, the critical section will not be released.
  • If the target thread is allocating memory from the heap, the heap lock will not be released.
  • If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
  • If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.

You ask:

Is there a way to interrupt doLaboriousTask2() and have it exit immediately?

Well, you could call TerminateThread but, for all the reasons described in the documentation, you almost certainly should not do that.

这篇关于Ç - 中断它是在一个线程中执行一个函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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