Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threadpool, order of execution and long running operations

I have a need to create multiple processing threads in a new application. Each thread has the possibility of being "long running". Can someone comment on the viability of the built in .net threadpool or some existing custom threadpool for use in my application?

Requirements :

Works well within a windows service. (queued work can be removed from the queue, currently running threads can be told to halt)

Ability to spin up multiple threads.

Work needs to be started in sequential order, but multiple threads can be processing in parallel.

Hung threads can be detected and killed.

EDIT:

Comments seem to be leading towards manual threading. Unfortunately I am held to 3.5 version of the framework. Threadpool was appealing because it would allow me to queue work up and threads created for me when resources were available. Is there a good 3.5 compatable pattern (producer/consumer perhaps) that would give me this aspect of threadpool without actually using the threadpool?

like image 716
BoxOfNotGoodery Avatar asked Feb 27 '23 04:02

BoxOfNotGoodery


2 Answers

Your requirements essentially rule out the use of the .NET ThreadPool;

It generally should not be used for long-running threads, due to the danger of exhausting the pool.

It does work well in Windows services, though, and you can spin up multiple threads - limited automatically by the pool's limits.

You can not guarantee thread starting times with the thread pool; it may queue threads for execution when it has enough free ones, and it does not even guarantee they will be started in the sequence you submit them.

There are no easy ways to detect and kill running threads in the ThreadPool

So essentially, you will want to look outside the ThreadPool; I might recommend that perhaps you might need 'full' System.Threading.Thread instances just due to all of your requirements. As long as you handle concurrency issues (as you must with any threading mechanism), I don't find the Thread class to be all that difficult to manage myself, really.

like image 187
Andrew Barber Avatar answered Mar 01 '23 12:03

Andrew Barber


Simple answer, but the Task class (Fx4) meets most of your requirements.

Cancellation is cooperative, ie your Task code has to check for it.
But detecting hung threads is difficult, that is a very high requirement anyway.

But I can also read your requirements as for a JobQueue, where the 'work' consists of mostly similar jobs. You could roll your own system that Consumes that queue and monitors execution on a few Threads.

like image 21
Henk Holterman Avatar answered Mar 01 '23 14:03

Henk Holterman