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

  • Context: C/C++ 
  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    C++ Thread Threads
Click For Summary
SUMMARY

The discussion addresses the challenge of terminating child threads (b and c) from another child thread (b) in a C++ program. The recommended solution involves using the thread IDs of the child threads, which should be stored in a global structure by the parent thread upon creation. The function pthread_kill() is suggested for terminating the threads, ensuring that the calling thread is the last to be killed. An alternative approach is to send a SIGUSER1 signal from thread b to the parent, prompting the parent to terminate all threads.

PREREQUISITES
  • Understanding of C++ threading concepts
  • Familiarity with POSIX threads (pthreads)
  • Knowledge of signal handling in C++
  • Experience with global data structures in multithreaded applications
NEXT STEPS
  • Research pthread_kill() function usage and best practices
  • Learn about signal handling in C++ with SIGUSER1
  • Explore thread management techniques in C++ using global structures
  • Investigate design patterns for safer thread termination in multithreaded applications
USEFUL FOR

C++ developers, software engineers working with multithreading, and anyone involved in thread management and synchronization in C++ applications.

Superposed_Cat
Messages
388
Reaction score
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
Can't you pass a message back to a and have a kill both threads when it receives the message?
 
  • Like
Likes   Reactions: DEvens

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 25 ·
Replies
25
Views
1K