473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ThreadPool wait for threads to finish

Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this
pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter
Nov 17 '05 #1
11 20305
just a thought ...

have each thread report to the main thread upon completion and then check
the thread count at that point to see if others are still running....instead
of a while loop
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter

Nov 17 '05 #2
Shardool Karnik <Th**********@gmail.com> wrote:
have each thread report to the main thread upon completion and then check
the thread count at that point to see if others are still running....instead
of a while loop


That would work - or make each thread "produce" on a semaphore, and
wait (from the main thread) until the semaphore had been hit the
appropriate number of times.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Hi,

If you want to execute them in sequence what you need to make a thread for?

what if you do this:
1- Create a queue with the correct delegates referencing the methods you
want to execute
2- Create a thread and start Pop-ing the delegates and executing in turn.

The only drawback may be that you cannot stop an individual item, you could
finish the thread but it would finish the entire execution, but you may find
other approach to execute the rest.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if
this pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter

Nov 17 '05 #4
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
If you want to execute them in sequence what you need to make a thread for?


He didn't say he wanted to execute them in sequence - he said he wanted
to wait until they were all done. That's perfectly reasonable. For
instance, you might be writing a web app which has to query several
different data sources, and you want to do that concurrently, but you
can't actually complete the request until all the separate queries are
complete.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Hi,

You are right, I have no idea where I get that idea from :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
If you want to execute them in sequence what you need to make a thread
for?


He didn't say he wanted to execute them in sequence - he said he wanted
to wait until they were all done. That's perfectly reasonable. For
instance, you might be writing a web app which has to query several
different data sources, and you want to do that concurrently, but you
can't actually complete the request until all the separate queries are
complete.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #6
I needed to do the same thing and came up with the following solution. I
don't know how effective it is in all cases, but it worked in my situation.
private static AutoResetEvent AllThreadsDone = new
AutoResetEvent(false);

public static bool WaitForThreadPoolIdle(int timeout)

{

bool ret = false;

Thread threadMonitor = new Thread(new
ThreadStart(MonitorThreads));

threadMonitor.Start();

ret = AllThreadsDone.WaitOne(timeout,false);

if (!ret)

{

threadMonitor.Abort();

}

return ret;

}

private static void MonitorThreads()

{

int mwt, miot, awt, aiot;

ThreadPool.GetMaxThreads(out mwt, out miot);

// make sure all the thread pool threads are done working

awt = aiot = 0;

while(mwt != awt || miot != aiot)

{

ThreadPool.GetAvailableThreads(out awt, out aiot);

Thread.Sleep(0);

}

Thread.Sleep(0);

AllThreadsDone.Set();

}

"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if
this pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter

Nov 17 '05 #7
Peter,

Are you opposed to creating a group of reset events?
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if
this pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter

Nov 17 '05 #8

"James Swindell" <ja******@hotmail.com> skrev i en meddelelse
news:OS**************@TK2MSFTNGP14.phx.gbl...
Peter,

Are you opposed to creating a group of reset events?


No, I am not opposed to that if it would get the job done, but I have now
found a solution where I use a "counting semaphore". When I create a thread
I increment the semphore count, and each thread gets a reference to the
semaphore which it signals (decrements) when done, and in my main thread I
wait for the semaphore count to reach 0 again.

Thanks,
Peter
Nov 17 '05 #9
"Howard Swope" <howardsnewsATspitzincDOTcom> skrev i en meddelelse
news:u5*************@TK2MSFTNGP09.phx.gbl...
I needed to do the same thing and came up with the following solution. I
don't know how effective it is in all cases, but it worked in my situation.
private static AutoResetEvent AllThreadsDone = new
AutoResetEvent(false);

public static bool WaitForThreadPoolIdle(int timeout)


<cut>

Hi, thanks for the suggestion. I have gone the "counting semaphore" route. I
just thought the thread-pool might offer an in-built wait-method or
something for that functionality.

Peter
Nov 17 '05 #10

"Jon Skeet [C# MVP]" <sk***@pobox.com> skrev i en meddelelse
news:MP************************@msnews.microsoft.c om...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
If you want to execute them in sequence what you need to make a thread
for?


He didn't say he wanted to execute them in sequence - he said he wanted
to wait until they were all done. That's perfectly reasonable. For
instance, you might be writing a web app which has to query several
different data sources, and you want to do that concurrently, but you
can't actually complete the request until all the separate queries are
complete.


Yes, I am actually querying a search engine with a large number of search
requests while I process a large batch of documents. I'm hoping that by
splitting the batch of documents up into a number of threads each performing
searches on a smaller subset of documents then I can increase throughput
(actually I know I can), but I need to wait until all the searches/threads
are complete before I continue processing the documents batch.

Peter
Nov 17 '05 #11

"Jon Skeet [C# MVP]" <sk***@pobox.com> skrev i en meddelelse
news:MP************************@msnews.microsoft.c om...
Shardool Karnik <Th**********@gmail.com> wrote:
have each thread report to the main thread upon completion and then check
the thread count at that point to see if others are still
running....instead
of a while loop


That would work - or make each thread "produce" on a semaphore, and
wait (from the main thread) until the semaphore had been hit the
appropriate number of times.


This is the route I have taken (a "counting semaphore", which I increment
each time I start a thread, and which each thread decrements when finished -
the main thread then waits until the count is 0).

Actually I didn't quite understand the first proposal. By "report to the
main thread", is that an event or some other sort of messaging mechanism
which the main thread listens for - obviously some sort of non-busy
listening?

Thanks,
Peter
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alban Hertroys | last post by:
Hello, I'm using psycopg to insert records in a number of threads. After the threads finish, another thread runs to collect the inserted data. Now, my problem is that psycopg let's my threads...
6
by: Quiet Man | last post by:
Hi all, I'm designing a fairly simple service that will run on W2K/SP4 and W2K3 servers. It's job is to be a very specialized database server that listens on a given IP address / TCP port and...
13
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled...
4
by: Lucas Tam | last post by:
Is there a document online which details the advantages of using a Threadpool? My application uses a user configuration amount of threads which does the following: Main Thread Gives Work to...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
5
by: =?Utf-8?B?RkxEYXZlTQ==?= | last post by:
I'm developing an application that gets data from 100 sources (via telnet connections, but you can think stock quotes from a webservice if you like). I was planning on using the thread pool (25...
1
by: EricBlair | last post by:
Hello, I've written a simple app as a test to run multiple threads from a pool. I'm able to do this, but what's happening is that the main thread finishes before all the workers do. So the...
3
by: UltimateBanoffee | last post by:
Hi, I'm using asp.net 2.0 and I have an understanding issue here! I don't quite understand when the available threads in the ThreadPool are ever used. The application I have running doesn't use...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.