Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the Task Parallel Library have a 'hidden' 1 second timeout for scheduling tasks under certain conditions?
    primarykey
    data
    text
    <p>My laptop has 2 logical processors and I stumbled upon the scenario where if I schedule 2 tasks that take longer than 1 second without designating them long-running, subsequent tasks are started after 1 second has elapsed. It is possible to change this timeout?</p> <p>I know normal tasks should be short-running - much shorter than a second if possible - I'm just wondering I am seeing hard-coded TPL behavior or if I can influence this behavior in any way other than designating tasks long-running.</p> <p>This Console app method should demonstrate the behavior for a machine with any number of processors:</p> <pre><code>static void Main(string[] args) { var timer = new Stopwatch(); timer.Start(); int numberOfTasks = Environment.ProcessorCount; var rudeTasks = new List&lt;Task&gt;(); var shortTasks = new List&lt;Task&gt;(); for (int index = 0; index &lt; numberOfTasks; index++) { int capturedIndex = index; rudeTasks.Add(Task.Factory.StartNew(() =&gt; { Console.WriteLine("Starting rude task {0} at {1}ms", capturedIndex, timer.ElapsedMilliseconds); Thread.Sleep(5000); })); } for (int index = 0; index &lt; numberOfTasks; index++) { int capturedIndex = index; shortTasks.Add(Task.Factory.StartNew(() =&gt; { Console.WriteLine("Short-running task {0} running at {1}ms", capturedIndex, timer.ElapsedMilliseconds); })); } Task.WaitAll(shortTasks.ToArray()); Console.WriteLine("Finished waiting for short tasks at {0}ms", timer.ElapsedMilliseconds); Task.WaitAll(rudeTasks.ToArray()); Console.WriteLine("Finished waiting for rude tasks at {0}ms", timer.ElapsedMilliseconds); Console.ReadLine(); } </code></pre> <p>Here is the app's output on my 2 proc laptop:</p> <pre><code>Starting rude task 0 at 2ms Starting rude task 1 at 2ms Short-running task 0 running at 1002ms Short-running task 1 running at 1002ms Finished waiting for short tasks at 1002ms Finished waiting for rude tasks at 5004ms Press any key to continue . . . </code></pre> <p>The lines:</p> <pre><code>Short-running task 0 running at 1002ms Short-running task 1 running at 1002ms </code></pre> <p>indicate that there is a 1 second timeout or something of that nature allowing the shorter-running tasks to get scheduled over the 'rude' tasks. That's what I'm inquiring about.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload