0

Even if I am using TPL from long time but since it sounds new to me. I want to understand the TPL with thread pool and I created a POC in .NET framework 4.0 for that which is as below.

 public class CustomData
 {
    public long CreationTime;
    public int Name;
    public int ThreadNum;
 }

 public class TPLSample
 {
    public int MaxThread = 0;
    public void Start()
    {
        Task[] taskArray = new Task[10000];
        for (int i = 0; i < taskArray.Length; i++)
        {
            taskArray[i] = Task.Factory.StartNew((Object obj) =>
            {
                var data = new CustomData() { Name = i, CreationTime = DateTime.Now.Ticks };
                Thread.SpinWait(10000);
                data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
                if (Thread.CurrentThread.ManagedThreadId > MaxThread)
                {
                    MaxThread = Thread.CurrentThread.ManagedThreadId;
                }
                Console.WriteLine("Task #{0} created at {1} on thread #{2}.",
                                  data.Name, data.CreationTime, data.ThreadNum);
            },
                                                 i);
        }
        Task.WaitAll(taskArray);
        Console.WriteLine("Max no of threads {0}", MaxThread);
    }
}

I found that only 14 threads are created to do this task!!

But why the 14? what is the criteria ? can I increase or decrease this number? How can I change this number. Is it really possible or totally abstracted from a developer.

D J
  • 6,908
  • 13
  • 43
  • 75

1 Answers1

2

From MSDN:

The number of operations that can be queued to the thread pool is limited only by available memory; however, the thread pool limits the number of threads that can be active in the process simultaneously. Beginning with the .NET Framework 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. A process can call the GetMaxThreads method to determine the number of threads.

Another MSDN:

The TPL may employ various optimizations, especially with large numbers of delegates.

Another SO question about this. Hopefully this will quench your thirst.

Community
  • 1
  • 1
techvice
  • 1,315
  • 1
  • 12
  • 24
  • But if it is based on system memory then how do we guaranty that code will run successfully on all the machines? As I might want to run maximum processes together. But might be the code written using TPL is independent of consideration that how many threads will be created. If one system allows 100 threads together it might be a problem. – D J Nov 21 '14 at 05:46
  • @DJ You can set the maximum process count by using `ParallelOptions.MaxDegreeOfParallelism` but I believe that unless the user has enough resources to maximize that thread pool determined number, that maximum number won't be used. In your question, I don't think it would be a good idea to code around the notion that you will always have a set number of threads. You can set a maximum number but beyond that, it's all dependent on the user system. – techvice Nov 21 '14 at 16:21