C语言 杀死 Pthread 库中的线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2084830/
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-09-02 04:10:43  来源:igfitidea点击:

Kill Thread in Pthread Library

cmultithreadingpthreadsposix

提问by Sajad Bahmani

I use pthread_create(&thread1, &attrs, //... , //...);and need if some condition occured need to kill this thread how to kill this ?

我使用pthread_create(&thread1, &attrs, //... , //...);并且需要如果发生某些情况需要杀死该线程如何杀死它?

回答by Antti Huima

First store the thread id

首先存储线程id

pthread_create(&thr, ...)

then later call

然后再打电话

pthread_cancel(thr)

However, this not a recommended programming practice! It's better to use an inter-thread communication mechanism like semaphores or messages to communicate to the thread that it should stop execution.

但是,这不是推荐的编程实践!最好使用诸如信号量或消息之类的线程间通信机制来通知线程应该停止执行。

Note that pthread_kill(...) does not actually terminate the receiving thread, but instead delivers a signal to it, and it depends on the signal and signal handlers what happens.

请注意 pthread_kill(...) 实际上并没有终止接收线程,而是向它传递一个信号,它取决于信号和信号处理程序会发生什么。

回答by alecov

There are two approaches to this problem.

有两种方法可以解决这个问题。

  • Use a signal: The thread installs a signal handler using sigaction()which sets a flag, and the thread periodically checks the flag to see whether it must terminate. When the thread must terminate, issue the signal to it using pthread_kill()and wait for its termination with pthread_join(). This approach requires pre-synchronization between the parent thread and the child thread, to guarantee that the child thread has already installed the signal handler before it is able to handle the termination signal;
  • Use a cancellation point: The thread terminates whenever a cancellation function is executed. When the thread must terminate, execute pthread_cancel()and wait for its termination with pthread_join(). This approach requires detailed usage of pthread_cleanup_push()and pthread_cleanup_pop()to avoid resource leakage. These last two calls might mess with the lexical scope of the code (since they may be macros yielding {and }tokens) and are very difficult to maintain properly.
  • 使用信号:线程安装一个信号处理程序,使用sigaction()它设置一个标志,并且线程定期检查该标志以查看它是否必须终止。当线程必须终止时,使用 向它发出信号pthread_kill()并等待其终止pthread_join()。这种方式需要在父线程和子线程之间进行预同步,以保证子线程在能够处理终止信号之前已经安装了信号处理程序;
  • 使用取消点:只要执行取消功能,线程就会终止。当线程必须终止时,执行pthread_cancel()并等待其终止pthread_join()。这种方法需要详细使用pthread_cleanup_push()pthread_cleanup_pop()避免资源泄漏。最后两个调用可能会干扰代码的词法范围(因为它们可能是宏生成{}标记)并且很难正确维护。

(Note that if you have already detached the thread using pthread_detach(), you cannot join it again using pthread_join().)

(请注意,如果您已经使用 分离线程pthread_detach(),则不能使用 再次加入它pthread_join()。)

Both approaches can be very tricky, but either might be specially useful in a given situation.

这两种方法都可能非常棘手,但在特定情况下可能特别有用。

回答by Fredrik Jansson

I agree with Antti, better practice would be to implement some checkpoint(s) where the thread checks if it should terminate. These checkpoints can be implemented in a number of ways e.g.: a shared variable with lock or an event that the thread checks if it is set (the thread can opt to wait zero time).

我同意 Antti,更好的做法是实现一些检查点,线程检查它是否应该终止。这些检查点可以通过多种方式实现,例如:带锁的共享变量或线程检查是否设置的事件(线程可以选择等待零时间)。

回答by Wyzard

Take a look at the pthread_kill()function.

看一下pthread_kill()功能。

回答by Anand Paul

pthread_exit(0) 

This will kill the thread.

这将杀死线程。