I am working on a project that deals with a few tasks running in parallel.
The number of tasks to be processed is generally ~200 and those of that can be parallel is about 10
I am considering using TPL because it simplifies a lot of details also because it provides a way to control the degree of concurrency with custom TaskScheduler and such.
However, I am also trying to find a nice to have feature that lets me know that a particular thread is asking for more work and that the ThreadPool’s queue is empty and it does not have any more work to steal from other threads.
Can this be done?
EDIT 1
I was not clear the first time. Each time I can only queue about 50 tasks to the ThreadPool and we don’t want to wait until the entire 50 items are done processing.
Unfortunately, the inner workings of the ThreadPool are not exposed publically. There is no way to hook into this specific information.
That being said, the ThreadPool works as a single cohesive unit – local work queues really only matter when a single thread pool thread starts a new Task, since the new task gets added to that thread’s local work queue. The “ThreadPool’s queue is empty and it does not have any more work to steal from other threads” will only occur when the entire ThreadPool is done processing, which, if you’re the one adding the work, will happen as soon as all of your work is done. This is fairly easy to check for…
Edit:
If you’re using the TPL, you can just queue up the entire workload. By using a custom TaskScheduler (or even the default, which would just rely on the ThreadPool’s pooling), you’ll automatically get pooling of resources, which prevents too many work items from being processed at once. As your work load processes, more items will automatically get scheduled…