Kill all parents child threads from a child thread in C++

In summary, the conversation discusses the issue of killing two threads, b and c, from a child thread in a C++ program. The challenge is that c is waiting for input using cin, so simple flags cannot be used. A possible solution is to pass a message back to the parent thread and have it kill both threads when it receives the message. Another suggestion is to store the thread IDs of b and c in a global structure and use pthread_kill() to terminate them. However, this is not considered a good program design. A better approach would be to send a SIGUSER1 signal from thread b to the parent, which would then call pthread_kill on all threads.
  • #1
Superposed_Cat
388
5
I have a C++ program that starts two threads from the parent thread, I need to kill both children from a child thread.

____> b
|
a -
|_____> c

I need to kill b & c from b. Issue is, I can't use simple flags, as c waits for input using cin.
Any help appreciated.
 
Technology news on Phys.org
  • #2
Can't you pass a message back to a and have a kill both threads when it receives the message?
 
  • Like
Likes DEvens
  • #3
You need the threadid of b and of c. Store them in global structure by parent as each thread is created. Then call pthread_kill() on each thread being sure to kill "yourself" last.
https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/ptkill.htm
This is not greatr program design BTW. A better approach might be to send a SIGUSER1 from thread b to parent, and then have the parent call pthread_kill on everybody.
 

What is a "Kill all parents child thread" in C++?

A "Kill all parents child thread" in C++ is a term used to describe a programming technique where a child thread is created within a parent thread and has the ability to terminate all of its parent's threads. This can be useful in certain situations where the parent threads need to be stopped abruptly.

How is a "Kill all parents child thread" created in C++?

To create a "Kill all parents child thread" in C++, the parent thread must first create a child thread using the std::thread class. Within the child thread, a function can be defined to terminate all of its parent's threads using the std::terminate function.

Can a "Kill all parents child thread" be used in any situation?

No, a "Kill all parents child thread" should only be used in specific situations where it is necessary to terminate all of the parent threads. In most cases, it is preferred to use other methods for thread management and termination.

What are the potential risks of using a "Kill all parents child thread" in C++?

One potential risk is that the child thread may accidentally terminate important or necessary parent threads, leading to unexpected program behavior. Additionally, using std::terminate can cause memory leaks and other errors if not used carefully.

Are there any alternatives to using a "Kill all parents child thread" in C++?

Yes, there are other methods for managing and terminating threads in C++. For example, using std::atomic variables can be a safer and more controlled way to stop threads. It is important to carefully consider the specific needs of your program before deciding on a thread management technique.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
Replies
3
Views
766
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top