[Mono-dev] Delay on first thread pool job

nedry brett.e.ernst at gmail.com
Thu Sep 22 17:39:27 EDT 2011


Hi all,
I was investigating a small delay that occurs when the first job is
submitted to the thread pool. This isn't a big deal for long-running apps,
but for small command-line utilities that use one or more async functions,
this can create a noticeable pause during startup.

If my theory is correct, it is due to the 500ms throttling in the
monitor_thread function in mono/metadata/threadpool.c. The throttle appears
to affect the first thread created and therefore the first job queued.

The function start_threadpool_idle_threads appears to work around this by
starting some threads up to the minimum, but its invocation on line 1060 was
commented out in a March 9 commit with some other changes. Without
understanding the reasoning, I'm not sure if it's safe to uncomment the
line.

It looks like start_threadpool_idle_threads is also invoked by the
SetMinThreads icall, so a workaround is illustrated in the following repro:

using System;
using System.Threading;

namespace scratch {
    class Program {
        static void Main () {
            var t = DateTime.Now;
            int worker, io;
            ThreadPool.GetMinThreads(out worker, out io);
            //ThreadPool.SetMinThreads(worker, io); // uncomment this to see
the difference
            ThreadPool.QueueUserWorkItem((o) =>
Console.WriteLine((DateTime.Now - t).TotalMilliseconds));
            Console.ReadLine();
        }
    }
}

-Brett


--
View this message in context: http://mono.1490590.n4.nabble.com/Delay-on-first-thread-pool-job-tp3834942p3834942.html
Sent from the Mono - Dev mailing list archive at Nabble.com.


More information about the Mono-devel-list mailing list