C++ 杀死一个正在运行的线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10737417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:23:51  来源:igfitidea点击:

Kill a running thread

c++windowsmultithreading

提问by Aneesh Narayanan

What happens if we forcefully kill a running thread

如果我们强行杀死一个正在运行的线程会发生什么

I have a thread namely RecordThread()which calls some complex and time consuming functions. In these functions I am using try-catchblocks, allocating and deallocation memory and using critical section variables etc.

我有一个线程RecordThread(),它调用一些复杂且耗时的函数。在这些函数中,我使用try-catch块、分配和释放内存以及使用临界区变量等。

like

喜欢

  void RecordThread()
  {
    AddRecord();
    FindRecord();
    DeleteRecord();
    // ...
    ExitThread(0);
   } 

After creating this thread, I am immediately killing it before the thread completes its execution. In this case what happens if the thread is forcefully killed? Do the internal functions (AddRecord, DeleteRecord) complete their execution after we killed the thread?

创建此线程后,我会在线程完成执行之前立即将其杀死。在这种情况下,如果线程被强行杀死会发生什么?在我们杀死线程后AddRecord,内部函数 ( , DeleteRecord) 是否完成了它们的执行?

回答by André Caron

After creating this thread, I am immediately killing it before the thread completes its execution.

创建此线程后,我会在线程完成执行之前立即将其杀死。

I assume you mean you are using TerminateThread()in the following fashion:

我假设您的意思是您TerminateThread()以以下方式使用:

HANDLE thread = CreateThread(...);

// ...
// short pause or other action?
// ...

TerminateThread(thread, 0); // Dangerous source of errors!
CloseHandle(thread);

If that is the case, then no, the thread executing RecordThread()will be stopped exactly where it is at the time that the other thread calls TerminateThread(). As per the notes in the TerminateThread()documentation, this exact point is somewhat random and depends on complex timing issues which are out of your control. This implies that you can't handle proper cleanup inside a thread and thus, you should rarely, if ever, kill a thread.

如果是这种情况,则不,正在执行的线程RecordThread()将准确地停止在另一个线程调用TerminateThread(). 根据TerminateThread()文档中的注释,这个确切的点有点随机,取决于您无法控制的复杂时序问题。这意味着您无法在线程内处理适当的清理工作,因此,您应该很少(如果有的话)杀死一个线程

The proper way to request the thread to finish is by using WaitForSingleObject()like so:

请求线程完成的正确方法是使用WaitForSingleObject()如下:

HANDLE thread = CreateThread(...);

// ...
// some other action?
// ...

// you can pass a short timeout instead and kill the thread if it hasn't
// completed when the timeout expires.
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);

回答by Vladimir Sinenko

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682659%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682659%28v=vs.85%29.aspx

ExitThread is the preferred method of exiting a thread in C code. However, in C++ code, the thread is exited before any destructors can be calledor any other automatic cleanup can be performed. Therefore, in C++ code, you should return from your thread function.

ExitThread 是在 C 代码中退出线程的首选方法。但是,在 C++ 代码中,线程会在调用任何析构函数或执行任何其他自动清理之前退出。因此,在 C++ 代码中,您应该从线程函数中返回。

However the function calls will of course be completed because they're called before the ExitThread().

然而,函数调用当然会完成,因为它们在 ExitThread() 之前被调用。

回答by Piotr Tkaczyk

killing thread is the last resort - as Andre stated it leaves data in unknown state, you should never do that if thread works on shared object. Better choices are to notify thread to finish work by:

杀死线程是最后的手段——正如安德烈所说,它使数据处于未知状态,如果线程在共享对象上工作,你不应该这样做。更好的选择是通过以下方式通知线程完成工作:

-using global volatile(important) variable which is changed only by main thread and tested by workers
-using signaltype synchronization objects (mainly Events) also set by main thread and tested by workers

-使用仅由主线程更改并由工作人员测试的全局易失性(重要)变量
-使用信号类型同步对象(主要是事件)也由主线程设置并由工作人员测试

回答by octavio oli

example of Thread that works well:

运行良好的线程示例:

definition in *.h ------------------------------------

DWORD WINAPI Th_EspectroIF(LPVOID lpData);

CThread th_espectro(Th_EspectroIF);

use in *.cc -----------------------------------

DWORD WINAPI Th_EspectroIF(LPVOID lpData)
{

//Your code...

th_espectro.Stop(true);//stop this Thread 

}

call the Thread with: th_espectro.Start();

调用线程:th_espectro.Start();