How to get the second System.Thread.ThreadPool?

If I use ThreadPool in a nested manner, my application freezes:

ThreadPool.QueueUserWorkItem((state) => ThreadPool.QueueUserWorkItem(Action)); 

How to get a second and independent ThreadPool to achieve nesting?

+4
source share
5 answers

There is only one ThreadPool - this is not something you can (or should) do more than one instance in an application.

I do not recommend doing this, but if you really wanted to, you could use multiple instances of your own ThreadPool implementation, such as SmartThreadPool . This would allow technically permitting separate “thread pools”.

However, I suspect that you were hanging due to deadlock , and not due to the use of ThreadPool. I would investigate where you get curls. The VS2010 concurrency renderer is very good for this if you have a copy of the beta version of VS 2010 installed.

+6
source

Your application is probably hanging because you are filling it to such an extent that all active threads are waiting in line for the queue. (I assume that the source thread expects the work item to complete the queue.)

ThreadPool defaults to 25 * workflows (number of processors) (IIRC).

You probably want to recycle the way you queue. If you put them in the queue, and then exit and exit the stream, you are fine, but the presence of work items that hang in anticipation of other processes is usually poor design; if this is what you are doing, you probably need to use real threads or a completely different design. Using real threads is probably no less a bad idea, because everything that will do (based on what I can guess about your use), you simply create a large number of threads that will not benefit your performance.

The best design may be to have some kind of queue or stack in which several workflows (2-4 depending on the number of processors) add elements and go to work. If an element needs to queue a new element, it simply adds it to the stack (and adds itself back to the stack with some kind of dependency tracking if it needs to wait for another element).

+4
source

You are using the wrong tools.

In .NET 4.0, they introduced a parallel task library. This allows you to do things like using multiple aad pools, as well as parent-child relationships between work items.

Start with the Task class, which replaces ThreadPool.QueueUserWorkItem.

http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx

EDIT

An example of creating your own thread pool using Task and TaskScheduler.

http://msdn.microsoft.com/en-us/library/dd997413(VS.100).aspx

+2
source

There is one and only one thread.

0
source

While the Windows API allows multiple thread pools to be used in a process , .NET does not directly expose this, however:

There is exactly one .NET ThreadPool for the .NET AppDomain and there is absolutely no way to change this.

Thus, with different application domains .. in the same process it is possible to have different .NET ThreadPools.

(NOTE: The heuristics for the Hill Climb algorithm used in .NET are also determined based on per-.NET ThreadPool. This, along with the ability to specify the maximum number of workers per type, is a disadvantage of the “global per appdomain” ThreadPool with a variety of workloads.)

0
source

Source: https://habr.com/ru/post/1300464/


All Articles