Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spawn Multiple Threads for work then wait until all finished

just want some advice on "best practice" regarding multi-threading tasks.

as an example, we have a C# application that upon startup reads data from various "type" table in our database and stores the information in a collection which we pass around the application. this prevents us from hitting the database each time this information is required.

at the moment the application is reading data from 10 tables synchronously. i would really like to have the application read from each table in a different thread all running in parallel. the application would wait for all the threads to complete before continuing with the startup of the application.

i have looked into BackGroundWorker but just want some advice on accomplishing the above.

  1. Does the method sound logical in order to speed up the startup time of our application
  2. How can we best handle all the threads keeping in mind that each thread's work is independent of one another, we just need to wait for all the threads to complete before continuing.

i look forward to some answers

like image 550
pharoc Avatar asked Mar 27 '10 10:03

pharoc


People also ask

How does one ensure thread Synchronisation such that all threads wait until all threads arrive?

Barriers. Barriers are a synchronization mechanism that can be used to coordinate multiple threads working in parallel. A barrier allows each thread to wait until all cooperating threads have reached the same point, and then continue executing from there.

What happens if you spawn too many threads?

If you create thousands of threads then you will waste time context switching between them and your work will take longer to complete. Instead of manually starting new threads you should use the thread pool to perform your work so Windows itself can balance the optimum number of threads.

Does Java wait for all threads to finish?

Behind the scenes it is using default JVM's fork join pool which means that it will wait for all the threads to finish before continuing.

What is the method to be called to make main thread wait until the completion of other threads?

The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution. wait() is mainly used for shared resources, a thread notifies other waiting thread when a resource becomes free. On the other hand join() is used for waiting a thread to die.


1 Answers

My preference for this is to handle this via a single WaitHandle, and use Interlocked to avoid locking on a counter:

class Program {     static void Main(string[] args)     {         int numThreads = 10;         ManualResetEvent resetEvent = new ManualResetEvent(false);         int toProcess = numThreads;          // Start workers.         for (int i = 0; i < numThreads; i++)         {             new Thread(delegate()             {                 Console.WriteLine(Thread.CurrentThread.ManagedThreadId);                 // If we're the last thread, signal                 if (Interlocked.Decrement(ref toProcess) == 0)                     resetEvent.Set();             }).Start();         }          // Wait for workers.         resetEvent.WaitOne();         Console.WriteLine("Finished.");     } } 

This works well, and scales to any number of threads processing, without introducing locking.

like image 106
Reed Copsey Avatar answered Oct 05 '22 06:10

Reed Copsey