C语言 如何从该循环之外杀死处于无限循环中的 pthread?

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

How can I kill a pthread that is in an infinite loop, from outside that loop?

cmemory-leakspthreadsthreadpool

提问by Pithikos

I create a thread and I put it into an infinite loop. I get memory leaks when checking the code with valgrind. Here is my code:

我创建了一个线程并将其放入一个无限循环中。使用valgrind检查代码时出现内存泄漏。这是我的代码:

#include <pthread.h>
#include <time.h>

void thread_do(void){
    while(1){}
}

int main(){
    pthread_t th;   
    pthread_create(&th, NULL, (void *)thread_do, NULL);

    sleep(2);
    /* I want to kill thread here */
    sleep(2);
    return 0;
}

So a thread is created in main and just runs thread_do() all the time. Is there a way to kill it from inside mainafter 2 seconds? I have tried both pthread_detach(th)and pthread_cancel(th)but I still get leaks.

所以在 main 中创建了一个线程,并且一直运行 thread_do()。有没有办法在 2 秒后从main内部杀死它?我已经尝试了这两种方法pthread_detach(th)pthread_cancel(th)但仍然有泄漏。

回答by Dmitri

As @sarnold pointed out, by default your thread can't be cancelled with pthread_cancel()without calling any functions that are cancellation points... but this can be changed by using pthread_setcanceltype()to set the thread's cancellation type to asynchronous instead of deferred. To do that, you'd add something like pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);near the start of your thread function, before you start the loop. You would then be able to terminate the thread by calling pthread_cancel(th)from main().

正如@sarnold 指出的那样,默认情况下,您的线程不能在pthread_cancel()不调用任何取消点函数的情况下取消……但这可以通过使用pthread_setcanceltype()将线程的取消类型设置为异步而不是延迟来更改。为此pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);,在开始循环之前,您需要在线程函数的开头附近添加类似的内容。然后您就可以通过调用pthread_cancel(th)from来终止线程main()

Note, though, that cancelling threads this way (whether asynchronous or not) doesn't clean up any resources allocated in the thread function (as noted by Kevin in a comment). In order to do this cleanly, you can:

但是请注意,以这种方式取消线程(无论是否异步)不会清除线程函数中分配的任何资源(正如 Kevin 在评论中指出的那样)。为了干净利落地做到这一点,您可以:

  • Ensure that the thread doesn't do anything it needs to clean up before exit (e.g. using malloc()to allocate a buffer)
  • Ensure that you have some way of cleaning up after the thread elsewhere, after the thread exits
  • Use pthread_cleanup_push()and pthread_cleanup_pop()to add cleanup handlers to clean up resources when the thread is cancelled. Note that this is still risky if the cancellation type is asynchronous, because the thread could be cancelled between allocating a resource and adding the cleanup handler.
  • Avoid using pthread_cancel()and have the thread check some condition to determine when to terminate (which would be checked in long-running loops). Since your thread then checks for termination itself, it can do whatever cleanup it needs to after the check.
  • 确保线程在退出前不做任何需要清理的事情(例如malloc()用于分配缓冲区)
  • 确保您在线程退出后,在其他地方的线程之后有某种清理方法
  • 使用pthread_cleanup_push()pthread_cleanup_pop()添加清理处理程序以在取消线程时清理资源。请注意,如果取消类型是异步的,这仍然存在风险,因为在分配资源和添加清理处理程序之间可能会取消线程。
  • 避免使用pthread_cancel()并让线程检查某些条件以确定何时终止(这将在长时间运行的循环中检查)。由于您的线程随后会检查终止本身,因此它可以在检查后进行任何需要的清理工作。

One way of implementing the last option is to use a mutex as a flag, and test it with pthread_mutex_trylock()wrapped in a function to use in the loop tests:

实现最后一个选项的一种方法是使用互斥锁作为标志,并使用pthread_mutex_trylock()包装在循环测试中的函数中对其进行测试:

#include <pthread.h>
#include <unistd.h>
#include <errno.h>

/* Returns 1 (true) if the mutex is unlocked, which is the
 * thread's signal to terminate. 
 */
int needQuit(pthread_mutex_t *mtx)
{
  switch(pthread_mutex_trylock(mtx)) {
    case 0: /* if we got the lock, unlock and return 1 (true) */
      pthread_mutex_unlock(mtx);
      return 1;
    case EBUSY: /* return 0 (false) if the mutex was locked */
      return 0;
  }
  return 1;
}

/* Thread function, containing a loop that's infinite except that it checks for
 * termination with needQuit() 
 */
void *thread_do(void *arg)
{
  pthread_mutex_t *mx = arg;
  while( !needQuit(mx) ) {}
  return NULL;
}

int main(int argc, char *argv[])
{
  pthread_t th;
  pthread_mutex_t mxq; /* mutex used as quit flag */

  /* init and lock the mutex before creating the thread.  As long as the
     mutex stays locked, the thread should keep running.  A pointer to the
     mutex is passed as the argument to the thread function. */
  pthread_mutex_init(&mxq,NULL);
  pthread_mutex_lock(&mxq);
  pthread_create(&th,NULL,thread_do,&mxq);

  sleep(2);

  /* unlock mxq to tell the thread to terminate, then join the thread */
  pthread_mutex_unlock(&mxq); 
  pthread_join(th,NULL);

  sleep(2);
  return 0;
}

If the thread is not detached (it generally isn't by default), you should call pthread_join()after stopping the thread. If the thread is detached, you don't need to join it, but you won't know exactly when it terminates (or even approximately, unless you add another way to indicate its exit).

如果线程未分离(默认情况下通常不是),则应pthread_join()在停止线程后调用。如果线程是分离的,您不需要加入它,但您不会确切知道它何时终止(甚至大约,除非您添加另一种方式来指示其退出)。

回答by sarnold

A few small thoughts:

几个小想法:

  1. You're trying to cancel your thread, but if the cancellation policy in place is for a deferred cancellation, your thread_do()will never be canceled, because it never calls any functions that are cancellation points:
  1. 您正在尝试取消您的线程,但如果取消政策适用于延迟取消,您thread_do()将永远不会被取消,因为它永远不会调用任何作为取消点的函数:
    A thread's cancellation type, determined by
    pthread_setcanceltype(3), may be either asynchronous or
    deferred (the default for new threads).  Asynchronous
    cancelability means that the thread can be canceled at any
    time (usually immediately, but the system does not guarantee
    this).  Deferred cancelability means that cancellation will
    be delayed until the thread next calls a function that is a
    cancellation point.  A list of functions that are or may be
    cancellation points is provided in pthreads(7).
  1. You're not joining the thread in your simple example code; call pthread_join(3)before the end of your program:
  1. 您没有在简单的示例代码中加入线程;pthread_join(3)在程序结束前调用:
    After a canceled thread has terminated, a join with that
    thread using pthread_join(3) obtains PTHREAD_CANCELED as the
    thread's exit status.  (Joining with a thread is the only way
    to know that cancellation has completed.)