Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written an application to see how we can use tasks.
I am starting 50000 tasks in one go with minimum thread pool set to 32500 ( which is the limit for a 64 bit application) and inserting a record into db in the function with a sleep of 5 sec.
If I set the min. thread pool to 8000 threads it is writing 15-16k records/minute.
But on increasing the no. of threads it reduces to 2-3k.
I am not able to pinpoint for the reason for this reduce.
i have built the app in vs 2012 .net 4.5.
What can be the reason for this?
Whether there is some other limit in .net or windows or some hardware related issue.

static void Main(string[] args)
       {
           int minThread = 32500;
           ThreadPool.SetMaxThreads(Int32.MaxValue, Int32.MaxValue);
           ThreadPool.SetMaxThreads(minThread, minThread);
           Start();
       }

       static void Start()
       {
           int count =50000 ;
           Task[] taskArray = new Task[count];
           for (int i = 0; i < taskArray.Length; i++)
           {
               Task task1 = null;
               task1 = Task.Factory.StartNew(() => function());
               taskArray[i] = task1;
           }
           Task.WaitAll(taskArray);
       }

       static void function()
       {
           do
           {
               Thread.Sleep(5000);
               using (TestEntities db = new TestEntities())
               {
                   Reponse res = new Reponse { datetime = DateTime.Now, message = Thread.CurrentThread.ManagedThreadId.ToString() };
                   db.Reponses.Add(res);
                   db.SaveChanges();
               }
           }
           while (true);
       }
Posted
Comments
Richard MacCutchan 29-Nov-14 4:51am    
The more threads you have the more you increase the overhead of process switching in the kernel. There is no simple way to determine the optimum number as it depends on the work each thread is doing.

1 solution

There are many reasons:

1) What "db" are you using and the performance characteristics of that engine.
2) Resource/object contention between your threads/tasks
3) The amount of data being saved

As a side note my RaptorDB - The Key Value Store V2[^] can handle 400,000 writes/sec on an i7 system, so that is some thing to think about if you really need high throughput writes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900