How to wait for thread?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

I'd like the main thread to wait until these three threads have
completed their task:

LoadOjbects lo = new LoadOjbects();

lo.thrd1.Start();
lo.thrd2.Start();
lo.thrd3.Start();

while (lo.thrd1.IsAlive || lo.thrd2.IsAlive || lo.thrd3.IsAlive)
{
Thread.Sleep(500);
}

The above works but adds a lot of time to the overall.

Thanks,
Brett
 
Brett Romero said:
I'd like the main thread to wait until these three threads have
completed their task:

LoadOjbects lo = new LoadOjbects();

lo.thrd1.Start();
lo.thrd2.Start();
lo.thrd3.Start();

while (lo.thrd1.IsAlive || lo.thrd2.IsAlive || lo.thrd3.IsAlive)
{
Thread.Sleep(500);
}

If you just want to wait for all three to end, without regard for the order
in which they end, you can just do:

lo.thrd1.Join();
lo.thrd2.Join();
lo.thrd2.Join();
// You won't get here until all 3 threads have completed
// ...but you can't tell which one completed first
// (nor does it matter).

-cd
 
Thanks. That seems to be working fine. I'm running different task on
each thread. It is taking the same amount of time as if I were doing
everything on the same thread. Here's what I'm doing in the
LoadObjects class:

thrd1 = new Thread( delegate( )
{
//do something here
} );

....and so on for the others

Why would it take the same amount of time? This is a single cpu (Intel
Pentium D 2.8Ghz) WinXP box.

Thanks,
Brett
 
Brett,

Well, you mentioned it yourself. You are on a single cpu. Because of
this, the tasks are all sharing the CPU. Each thread will get a slice of
CPU time.

Using threads doesn't guarantee that your program will run faster. Even
if you had a multi-cpu system, it isn't guaranteed. You could have slower
performance due to context switches, for example.

Hope this helps.
 
Hope this helps.

Yes. I was hoping hyperthread or something would come into play but I
guess it more just moving things around to different slices of
execution.

Thanks,
Brett
 
Are there any single core cpus that multi threading can take advantage
of?

Thanks,
Brett
 
Brett Romero said:
Are there any single core cpus that multi threading can take advantage
of?

You can get some performance gains from a single core hyperthread CPU - if
your program actually has real opportunities for parallelism in it. If your
program is I/O intensive (e.g. reading and writing a lot of files), then
you're unlikely to see significant improvement with multi-threading.

-cd
 
Lots of loading data from the data base (read/writes) for each thread.
So I guess that won't help much.

Thanks,
Brett
 
Brett Romero said:
Lots of loading data from the data base (read/writes) for each thread.
So I guess that won't help much.

It still might - only testing will tell. If your database is remote
(accessed over a network), then there may be significant opportunities for
improvement parallelism due to network latency (as opposed to actual I/O).
For example, I recently made a change to a web/database app to issue a
series of queries to the database in parallel rather than one after another.
The same total amount of I/I occurred, but the parallelism effectively
removed the network latency from the equation, so the overall process sped
up almost linearly up to ~25 parallel requests. If your database is local,
or you're interacting with local files, then latency is likely not a major
factor and parallel execution will probably have little impact.

-cd
 
It still might - only testing will tell. If your database is remote
(accessed over a network), then there may be significant opportunities for
improvement parallelism due to network latency (as opposed to actual I/O).
For example, I recently made a change to a web/database app to issue a
series of queries to the database in parallel rather than one after another.
The same total amount of I/I occurred, but the parallelism effectively
removed the network latency from the equation, so the overall process sped
up almost linearly up to ~25 parallel requests. If your database is local,
or you're interacting with local files, then latency is likely not a major
factor and parallel execution will probably have little impact.


The database is remote (local network). I don't see any improvement on
my machine. A co worker gets about a 5 second gain. So from 29
seconds down to 24.3. He has a a single Centrino Mobile Pentium M 2 G
processor (laptop).

On my machine, it is 31 and 32.7 (a small increase in time with multi
threading).

On another co workers (slow) machine, it is 2 minutes 48 seconds down
to 58 seconds.

Why do you think there are such differences between our machines?

Thanks,
Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top