c - interrupt a function call which is executing in a thread

五迷三道 提交于 2019-12-03 21:35:59

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!