QueueUserWorkItem() performance issues using Mono C#

676 Views Asked by At

I'm getting intermittent spikes in my profiler around the call to QueueUserWorkItem(). I queue roughly 20 jobs every second. Each job is roughly identical in terms of complexity. 95% of the jobs spend less than 0.01ms in the call to QueueUserWorkItem. But every few seconds, seemingly at random, a job will take from 20-60ms!

I'm trying to build a smooth simulation and just queuing my background tasks is causes serious hitches in my framerate.

This is not the time it takes for the job to actually FINISH, this is time spent on simply QUEUEING the job. Which I would expect should take virtually no time at all, so this is very frustrating.

foreach ( Job j in jobs_to_process )
{
    Job current_job = j;
    if ( !current_job.queued )
    {
        current_job.queued = true;

        Profiler.BeginSample("QueueWorkItem");
        ThreadPool.UnsafeQueueUserWorkItem( current_job.DoCalculation, null );
        Profiler.EndSample();
    }
}

//now I remove queued items from jobs_to_process, move them to jobs_in_progress list

Things I've tried:

  • Queuing fewer jobs per second only causes proportionately fewer hitches, but it still hitches.
  • Putting lock(obj) around all access to job.queued variable
  • Using SmartThreadPool instead of defaultThreadPool. Problem still persists inside STP's QueueWorkItem()
1

There are 1 best solutions below

1
On

Try with the --server flag when running mono (only compatible with mono master, as it was committed recently).