Strategy for avoiding threadpool starvation while performing cpu bound jobs in a queued fashion My aim is to avoid using threadpool threads for CPU bound work, thus avoiding a situation where IIS stops responding to new requests.

Can you see any problems with the code below? Is this a safe/clean approach? Can you offer any improvements?

    private static ConcurrentQueue&lt;Job&gt; Jobs = new ConcurrentQueue&lt;Job&gt;();
    static int threadCount = 0;

    private void QueueJob(Job job)
    {

        bool startQueue=false;

        lock(Jobs)
        {
            if (threadCount == 0)
            {
                Interlocked.Increment(ref threadCount);
                startQueue = true;
            }
        }

        Jobs.Enqueue(job);

        if (startQueue)
        {
           var t= new Thread(new ThreadStart(ConsumeQueue));              
           t.Start();
        }



    }
    private void ConsumeQueue()
    {
        while (true)
        {
            lock (Jobs)
            { 
                if (!Jobs.Any())
                {
                    Interlocked.Decrement(ref threadCount);
                    return;
                }
            }

            Job j;

            var jobToDo = Jobs.TryDequeue(out j);

            if (jobToDo)
            {
                DoCPUBoundWork(j);
            }
        }

    }
I think a much easier solution is to just increse the number of threads available in the thread pool by calling ThreadPool.SetMaxThreads().

Also, if you have this many threads, then it means that:


either you're not going to get any performance improvements from parallelization, because all your cores are busy
or most of your threads are blocked, which means a better approach would be to actually solve the problem by using asynchronous waiting (possibly with the help of C# 5 async-await), instead of working around the problem.