I have a multi-thread application where each thread has a helper thread that helps the first one to accomplish a task. I would like that when a thread is terminated (likely calling exit) the helper thread is terminated as well.
我有一个多线程应用程序,其中每个线程都有一个帮助程序线程,帮助第一个完成任务。我希望当一个线程被终止(可能是调用exit)时,helper线程也会被终止。
I know that there is the possibility to use exit_group
, but this system call kills all threads in the same group of the calling thread. For example, if my application has 10 threads (and therefore 10 additional helper threads) I would like that only the thread and the helper thread associated is terminated, while the other threads keep on running.
我知道有可能使用exit_group,但是这个系统调用会杀死调用线程的同一组中的所有线程。例如,如果我的应用程序有10个线程(因此有10个额外的帮助程序线程),我希望只有线程和辅助线程关联才能终止,而其他线程继续运行。
My application works exclusively on Linux.
我的应用程序专门用于Linux。
How can I have this behavior?
我怎么能有这种行为?
Reading around about multithreading I got a bit confused about the concept of thread group and process group in Linux. Are these terms referring to the same thing? Precisely, the process group (and perhaps the thread group) is the pid retrieved by one of the following calls :
阅读有关多线程的内容我对Linux中的线程组和进程组的概念感到有些困惑。这些术语是指同一件事吗?确切地说,进程组(可能是线程组)是由以下调用之一检索的pid:
pid_t getpgid(pid_t pid);
pid_t getpgrp(void); /* POSIX.1 version */
pid_t getpgrp(pid_t pid); /* BSD version */
1 个解决方案
#1
1
You are a bit adrift here. Forget exit_group
, which these days is the same as exit
on linux is not what you are looking for. Similarly the various get-pid calls aren't really what you want either.
你在这里有点漂亮。忘记exit_group,这些天与linux上的退出相同并不是你想要的。同样,各种get-pid调用也不是你想要的。
The simplest (and usually best) way to handle this is have each primary thread signal its helper thread to shut down and then pthread_join
it - or not if it is detached.
处理这个问题的最简单(通常是最好的)方法是让每个主要线程发出信号,让它的helper线程关闭,然后pthread_join它 - 如果它已经分离,则不会。
So something like:
所以类似于:
(a) primary work thread knows - however it knows - its work is done.
(a)主要工作线程知道 - 但是它知道 - 它的工作已经完成。
(b) signals helper thread via a shared switch or similar mechanism
(b)通过共享交换机或类似机制发出辅助线程信号
(c) helper thread periodically checks flag, cleans up and calls pthread_exit
(c)辅助线程定期检查标志,清理并调用pthread_exit
(d) primary worker thread calls pthread_join
(or not) on dead helper thread
(d)主要工作者线程在死辅助线程上调用pthread_join(或不调用)
(e) primary worker cleans up and calls pthread_exit
on itself.
(e)主要工作人员清理并自行调用pthread_exit。
There are a lot of variations on that but that's the basic idea. Beyond that you get into things like pthread_cancel
and areas you may want to avoid if you don't absolutely require them (and the potential headaches).
有很多变化,但这是基本的想法。除此之外,如果你不是绝对需要它们(以及潜在的头痛),你会遇到pthread_cancel和你可能想要避免的区域。
#1
1
You are a bit adrift here. Forget exit_group
, which these days is the same as exit
on linux is not what you are looking for. Similarly the various get-pid calls aren't really what you want either.
你在这里有点漂亮。忘记exit_group,这些天与linux上的退出相同并不是你想要的。同样,各种get-pid调用也不是你想要的。
The simplest (and usually best) way to handle this is have each primary thread signal its helper thread to shut down and then pthread_join
it - or not if it is detached.
处理这个问题的最简单(通常是最好的)方法是让每个主要线程发出信号,让它的helper线程关闭,然后pthread_join它 - 如果它已经分离,则不会。
So something like:
所以类似于:
(a) primary work thread knows - however it knows - its work is done.
(a)主要工作线程知道 - 但是它知道 - 它的工作已经完成。
(b) signals helper thread via a shared switch or similar mechanism
(b)通过共享交换机或类似机制发出辅助线程信号
(c) helper thread periodically checks flag, cleans up and calls pthread_exit
(c)辅助线程定期检查标志,清理并调用pthread_exit
(d) primary worker thread calls pthread_join
(or not) on dead helper thread
(d)主要工作者线程在死辅助线程上调用pthread_join(或不调用)
(e) primary worker cleans up and calls pthread_exit
on itself.
(e)主要工作人员清理并自行调用pthread_exit。
There are a lot of variations on that but that's the basic idea. Beyond that you get into things like pthread_cancel
and areas you may want to avoid if you don't absolutely require them (and the potential headaches).
有很多变化,但这是基本的想法。除此之外,如果你不是绝对需要它们(以及潜在的头痛),你会遇到pthread_cancel和你可能想要避免的区域。