ChildThread inside a Parent Thread - nsthread

Can you please help me in this:
How to create a child thread in a parent thread. And the parent thread should be terminated only after the child thread is terminated. and the parent thread is completed only after completing child thread.

Use NSCondition. Have the child thread signal the condition on termination. Have the parent wait on the condition. Add a predicate (a bool will do it) to work around the 'spurious signals' issues. See Using Conditions on the Threading Programming Guide.

Related

Kill parent and child thread by reading status of a variable from Child thread in python

My program takes input as a json file and runs various functions on the parameters by creating threads for each argument passed.Now i want to create sub-threads from these multiple parent threads which would monitor an alarm, if the alarm goes off the sub-thread and its corresponding parent thread should stop.
So the Child thread is an alarm monitoring thread if it sees the alarm is True it should kill the corresponding parent thread without affecting any other threads.
The flow is Flow of THREADS

How does Rust handle killing threads?

Is there a parent-child connection between threads that are spawned?
If I kill the thread from where I spawned other threads, are those going to get killed too? Is this OS specific?
How does Rust handle killing threads?
It doesn't; there is no way to kill a thread.
See also:
How to terminate or suspend a Rust thread from another thread?
How to check if a thread has finished in Rust?
Is there a parent-child connection between threads that are spawned?
When you spawn a thread, you get a JoinHandle that allows you to wait for the child thread to finish. The child does not know of the parent.
[what happens to the other threads] in the context of a thread panicking and dying
The documentation for thread::spawn covers this well:
The join handle will implicitly detach the child thread upon being
dropped. In this case, the child thread may outlive the parent (unless
the parent thread is the main thread; the whole process is terminated when
the main thread finishes). Additionally, the join handle provides a join
method that can be used to join the child thread. If the child thread
panics, join will return an Err containing the argument given to
panic.
That is, once a child thread has been started, what happens to the parent thread basically doesn't matter, unless the parent thread was the main thread, in which case the entire process is terminated.

Can we use Pthread_cond_signal() between between a thread and a child of same parent?

I have a parent process and I have created a new thread from the parent process using pthread_create(). I have also created a child process 'C' from the parent Process using fork() and exited the parent process. Now the child is running as daemon.
Can I use pthread_cond_signal between child process and the thread? Or can pthread_cond_signal be used only between threads of the same process?
You can use condition variables across processes if you make the condition variable process-shared, using condition variable attribute configured with the pthread_condattr_setpshared function and a value of PTHREAD_PROCESS_SHARED. You will also have to make the associated mutex process-shared, using a mutex attribute configured with pthread_mutexattr_setpshared.
By default, condition variables and mutexes cannot be shared across processes.

how child thread status is collected in parent thread when child reaches exit before the parent reaches join?

In my scenario, there is main thread which is creating 3 child threads using pthread_create().
After creating child threads, child threads tasks are completed and reached pthread_exit() statement but parent still not reached the statement pthread_join() to collect the status of childs.
But somehow at the end, I am able to get the status of the threads in the parent thread.
How it is possible, please can help me in this?
So, does it mean pthread_exit() will be in waiting state until the
parent collects status?
pthread_exit() ends; the thread isn't in waiting state, but rather a "zombie thread", as in this note:
Failure to join with a thread that is joinable (i.e., one that is not
detached), produces a "zombie thread". Avoid doing this, since each
zombie thread consumes some system resources, and when enough zombie
threads have accumulated, it will no longer be possible to create new
threads (or processes).
(The retval is stored inside the pthread_t buffer.)

Thread deletion design

I have multi thread program. I have a design of my application as follows:
Suppose one is main thread, and other are slave threads. Main thread keep track of all slave thread ID's. During one of the scenario of application (one of the scenario is graceful shutdown of application), i want to delete slave threads from main thread.
Here slave threads may be executing i.e., either in sleep mode or doing some action which i cannot stop the action. So i want to delete the threads from main thread with thread IDs i stored internally.
Additional info:
While deleting i should not wait for thread current action to complete as it may take long time as i am reading from data base and taking some action in thread, in case of gracefull shut down i should not wait for action to complete as it may take time.
If i force delete a thread how can there will be a resource leaks?
Is above design is ok or there is any flow or any ways we can improve the design.
Thanks!
It's not okay. It's a bad practice to forcefully kill a thread from another thread because you'll very likely to have resource leaks. The best way is to use an event or signal to signal the client process to stop and wait until they exit gracefully.
The overall flow of the program would look like this:
Parent thread creates an event (say hEventParent). it then creates child threads and passes hEventParent as a parameter. The Parent thread keeps the hThread of the child thread(s).
Child threads do work but periodically waits for hEventParent.
When the program needs to exit, the parent thread sets hEventParent. It then waits for hThread (WaitForMultipleObjects also accepts hThread)
Child thread is notified then execute clean up routine and exits.
When all the threads exit, the parent can then exit.
The most common approach consists in the main thread sending a termination signal to all the threads, then waiting for the threads to end.
Typically the worker threads will have a loop, inside of which the work is done. You can add a boolean variable that indicates if the thread needs to end. For example:
terminate = false;
while (!terminate) {
// work here
}
If you want your worker threads to go to sleep when they have no work, then it gets a bit more complicated. In this case you could make the threads wait on semaphores. Each semaphore will be signaled when there is work to do, and that will awaken the thread. You will also signal the semaphore when the request to terminate is issued. Example worker thread:
terminate = false;
while (!terminate) {
// work here
wait(semaphore); // go to sleep
}
When the main thread wants to exit it will set terminate to true for all the threads and then signal the thread semaphores to awaken the threads and give them a chance to see the termination request. After that it will join all the threads, and only after all the threads are finished it will exit.
Note that the terminate boolean may need to be declared as volatile if you are using C/C++, to indicate to the compiler that it may be changed from another thread.

Resources