Threads and ThreadPool's

  • Thread starter Thread starter Max Adams
  • Start date Start date
M

Max Adams

Threads and ThreadPool's

If I use a ThreadPool how can I tell when a thead in the threadpool has
exited? I don't want to set a global member variable I would much rather be
able to act on an event

Also (failing this ThreadPool issue) is it possible to create an array of
Worker Threads, can anyone illustrate with code please

Thanks
PT
 
Max,

If you want to be notified when a thread in the ThreadPool exits, you
would have to do it the same way that you would be notified when a thread
exits. You would have to write code at the end of your method which would
trigger some sort of notification event. This could be a delegate, an
object you pass in to have a method called on it, etc, etc.

If you wanted to create an array of worker threads, you can do it like
creating an array of any other type, but in this case, I would advise
against it. You would have to handle all the management yourself, and you
are going to gain little from writing the code to determine which thread to
use, keeping the thread alive, etc, etc.

Hope this helps.
 
I've done alot of Threaded code and I agree totally with Nicholas... Avoid
writing your own ThreadPool stuff if possible; the .NET ThreadPool is playing
games behind the scenes to be very efficient...

As far as getting notification of completion it depends on your needs.

If you can't sleep while the task runs {aka a GUI thread kicks off the task}
then kick off the task and have the last step of the task be to call
BeginInvoke on a an event that signals the GUI that the task has finished.

If you just want to know when a single task has completed then queue the
task and have the tasks last step be to set an event that wakes you up when
it's finished.

--Richard
 
Richard said:
I've done alot of Threaded code and I agree totally with Nicholas...
Avoid writing your own ThreadPool stuff if possible; the .NET
ThreadPool is playing games behind the scenes to be very efficient...

On the other hand, it's completely out of your control and is used by
other parts of the framework, which make it somewhat dangerous in some
ways.

Personally I wish the .NET framework had a flexible ThreadPool which
could be created by an individual app and used exclusively by the app.

As it is, I don't see much wrong with using your own ThreadPool,
assuming you get a suitably robust implementation. You can then decide
what it should do in terms of security contexts etc as well. Yes, the
system thread pool has been written to be very efficient - but a lot of
people don't need quite that level of efficiency. Better to work
solidly at 95% of the optimum speed than run at 100% for a while and
then accidentally fill up the system threadpool in a way which is
unpredictable and deadlocks all the threads.
 
One of my suggestion on the thread pool is to make sure you don't
create too many threads. Use the default 25 threads or lesser and
write your own function to control the pooling so that you keep only
required number of threads running all the time.
Nicholas Paldino said:
Max,

If you want to be notified when a thread in the ThreadPool exits, you
would have to do it the same way that you would be notified when a thread
exits. You would have to write code at the end of your method which would
trigger some sort of notification event. This could be a delegate, an
object you pass in to have a method called on it, etc, etc.

If you wanted to create an array of worker threads, you can do it like
creating an array of any other type, but in this case, I would advise
against it. You would have to handle all the management yourself, and you
are going to gain little from writing the code to determine which thread to
use, keeping the thread alive, etc, etc.

Hope this helps.


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

Max Adams said:
Threads and ThreadPool's

If I use a ThreadPool how can I tell when a thead in the threadpool has
exited? I don't want to set a global member variable I would much rather be
able to act on an event

Also (failing this ThreadPool issue) is it possible to create an array of
Worker Threads, can anyone illustrate with code please

Thanks
PT
 
On the other hand, it's completely out of your control and is used by
other parts of the framework, which make it somewhat dangerous in some
ways.

Agreed sort of: I don't like that I can't abort thread pool threads, and I
don't like that I can't join on thread pool threads, etc... but it's not out
of your control.

For most cases I've seen the thread pool does very well. The big thing I
see in people's code is that they tend to write overly complicated
synchronization logic rather than learn how to use
RegisterWaitForSingleObject() properly...
Personally I wish the .NET framework had a flexible ThreadPool which
could be created by an individual app and used exclusively by the app.

For what I'm working on right now I have pondered creating just such a more
flexible thread pool. A thread pool where I could Abort threads, set/change
base priorities, num min & max threads, automatically synchronize batch
start, and where I could WaitAll on an arbitrarily defined batch...
then accidentally fill up the system threadpool in a way which is
unpredictable and deadlocks all the threads.

Sounds like a bug in code to me - have you ever actually siezed up the pool,
I've never seen that?

--Richard
 
Richard said:
Agreed sort of: I don't like that I can't abort thread pool threads,

I don't have a problem with that, as I think aborting threads should
always be a last resort which should only be used to abort the whole
app. Aborting threads can leave the system in an ill-defined state,
where you don't want to continue with the application anyway.
and I don't like that I can't join on thread pool threads, etc... but
it's not out of your control.

It is - you can't dictate the thread creation or termination policy.
You can't (easily) dictate the size. You don't get to easily control
For most cases I've seen the thread pool does very well. The big thing I
see in people's code is that they tend to write overly complicated
synchronization logic rather than learn how to use
RegisterWaitForSingleObject() properly...


For what I'm working on right now I have pondered creating just such a more
flexible thread pool. A thread pool where I could Abort threads, set/change
base priorities, num min & max threads, automatically synchronize batch
start, and where I could WaitAll on an arbitrarily defined batch...

I've started work on a ThreadPool myself, although there's definitely
more to add. It's available at

http://www.yoda.arachsys.com/csharp/miscutil/src/MiscUtil/Threading/Cus
tomThreadPool.cs

If you have a look at it and spot any bugs, please let me know :)

(There's definite refactoring needed in a few places - WorkerThreadLoop
is too big and complicated, for a start.)
Sounds like a bug in code to me - have you ever actually siezed up the pool,
I've never seen that?

I haven't personally, although I've seen others complaining of it - and
it doesn't have to be a bug. If you've got some threads in the
threadpool waiting for other operations to complete, and those
operations are waiting for a threadpool thread to execute on, you can
end up with an effective deadlock. Don't forget that you don't always
know which operations use the ThreadPool. For instance, I'm pretty sure
that HttpWebRequest does. That's what I mean by the system ThreadPool
being out of developers' control.
 
Back
Top