Threading and Join

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

I need to start 8 threads and have the app or calling thread wait until all
threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some help.

Thanks,
Joe
 
Joe said:
I need to start 8 threads and have the app or calling thread wait until all
threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe

How about calling Join after you start all of the threads?

Mythran
 
Joe,

What you want to do here is call Start on all of your threads first, and
then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.
 
Hi Nicholas,

I had tried something similar with a join but the first join doesn't return
until the thread is complete.

Thread t = new Thread(new ThreadStart(Test) )
Thread t2 = new Thread(new ThreadStart(Test) )

t.Start();
t2.Start();

t.Join();
t2.Join();

Thanks,
Joe

Nicholas Paldino said:
Joe,

What you want to do here is call Start on all of your threads first,
and then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
I need to start 8 threads and have the app or calling thread wait until
all threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe
 
Unfortunately .NET doesn't have a built in semaphore primitive but you can
acheive the same effect with a counter and a ManualResetEvent. The algorithm
below can easily be encapsulated in a Semaphore class {I have one somewhere}:

You will need two variables at a scope that both the main thread and the
worker threads can access:

int numActiveThreads = 0;
ManualResetEvent waitEvent = new ManualResetEvent(false);

Have the MAIN THREAD issue Interlocked.Increment(&numActiveThreads) and then
spin off a worker thread - IN THAT ORDER - increment on main thread/spin off
worker until the desired number of threads have been started. After the
desired number of threads have been started let the main thread sleep on the
unsignalled event.

As each worker thread finishes have it issue
Interlocked.Decrement(&numActiveThreads) and test the result for <= 0. If
<= 0 then the last thread is finishing and you can wake the main thread...

--Richard
 
Joe,

You are right, the first join doesn't return until the thread is
complete. But that doesn't mean that the second thread stops running.
Also, when you call Join on the second thread, if it completed already, then
the call to Join will return automatically.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
Hi Nicholas,

I had tried something similar with a join but the first join doesn't
return until the thread is complete.

Thread t = new Thread(new ThreadStart(Test) )
Thread t2 = new Thread(new ThreadStart(Test) )

t.Start();
t2.Start();

t.Join();
t2.Join();

Thanks,
Joe

Nicholas Paldino said:
Joe,

What you want to do here is call Start on all of your threads first,
and then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
I need to start 8 threads and have the app or calling thread wait until
all threads are done. I was thinking of using Join but that will block
the thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe
 
Good point. I didn't think about that.

Thanks again,
Joe

Nicholas Paldino said:
Joe,

You are right, the first join doesn't return until the thread is
complete. But that doesn't mean that the second thread stops running.
Also, when you call Join on the second thread, if it completed already,
then the call to Join will return automatically.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
Hi Nicholas,

I had tried something similar with a join but the first join doesn't
return until the thread is complete.

Thread t = new Thread(new ThreadStart(Test) )
Thread t2 = new Thread(new ThreadStart(Test) )

t.Start();
t2.Start();

t.Join();
t2.Join();

Thanks,
Joe

Nicholas Paldino said:
Joe,

What you want to do here is call Start on all of your threads first,
and then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to start 8 threads and have the app or calling thread wait until
all threads are done. I was thinking of using Join but that will block
the thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe
 
Back
Top