Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block the main thread until all the other threads finish executing?

Tags:

python

This is a newbie question. I start 10 threads from my main thread. How do I stop the main thread from continuing until all the other threads finish?

like image 795
Bruce Avatar asked Jun 03 '11 12:06

Bruce


People also ask

How do you make a main thread wait until all other threads finished execution?

Waiting for existing threads to complete their execution can be achieved by using the awaitTermination() method.

What is the code if main thread should wait until all the other threads are finished in C?

So, if we want that the main thread should wait until all the other threads are finished then there is a function pthread_join(). #include <pthread. h> int pthread_join(pthread_t thread, void **rval_ptr); The function above makes sure that its parent thread does not terminate until it is done.

Which of the following correctly Forces thread until finishing the other thread execution?

Simply put, calling wait() forces the current thread to wait until some other thread invokes notify() or notifyAll() on the same object. For this, the current thread must own the object's monitor.

Which of the following could be used to keep the main thread alive until all other threads have terminated?

join() is a method in Java that causes the current thread to pause execution until the specified thread terminates. This is useful when we want to wait for a specific thread to complete before continuing the execution of the current thread.


1 Answers

Join all threads:

for t in threads:
    t.join()

Here threads is the list of your threads.

like image 191
Sven Marnach Avatar answered Oct 10 '22 01:10

Sven Marnach