HiveBrain v1.2.0
Get Started
← Back to all entries
principlecsharpMinor

Strategy for avoiding threadpool starvation while performing cpu bound jobs in a queued fashion

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
performingthreadpoolboundwhilequeuedstarvationjobsfashionforstrategy

Problem

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 Jobs = new ConcurrentQueue();
    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);
            }
        }

    }

Solution

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.

Context

StackExchange Code Review Q#19489, answer score: 2

Revisions (0)

No revisions yet.