Need Help Understanding .net ThreadPool

I am trying to understand what ThreadPool does, I have this .NET example:

class Program
{
    static void Main()
    {
        int c = 2;

        // Use AutoResetEvent for thread management

        AutoResetEvent[] arr = new AutoResetEvent[50];

        for (int i = 0; i < arr.Length; ++i)
        {
            arr[i] = new AutoResetEvent(false);
        }

        // Set the number of minimum threads
        ThreadPool.SetMinThreads(c, 4);

        // Enqueue 50 work items that run the code in this delegate function
        for (int i = 0; i < arr.Length; i++)
        {
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                Thread.Sleep(100);
                arr[(int)o].Set(); // Signals completion

            }, i);
        }

        // Wait for all tasks to complete
        WaitHandle.WaitAll(arr);
    }
}

Does this accomplish 50 “tasks,” in groups of 2 ( int c), until they end? Or I don’t understand what he is actually doing.

+3
source share
4 answers

By setting the minimum number of threads, the only thing you ask to do in the .NET environment is to allocate at least 2 threads to the thread pool. You do not ask him to limit himself to only 2.

Thus, there is no guarantee how many threads, in particular, your program will use for this. It depends on your system and many other factors.

, ( , , ), 4 , 3 , 7 , 10 ..

.

?

+1
+3

MSDN SetMinThreads:

, .

, , 2 . ( , , ), , .

, , , Process Monitor. , , .. , ( ).

, .

0

: , .NET , . , , , , . , , , .

SetMinThreads() , " ", - .

, , , Thread.Start. , MaxThreads, ( , 250 , , ), . , , , , - , , 5 , , . MaxThreads ; , .

, 50 , 50 ; , 250 , , . 250 , ; , , , "" , , , , reset .

0

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


All Articles