简体   繁体   English

使用Mono C#的QueueUserWorkItem()性能问题

[英]QueueUserWorkItem() performance issues using Mono C#

I'm getting intermittent spikes in my profiler around the call to QueueUserWorkItem(). 在调用QueueUserWorkItem()时,我的探查器中出现了间歇性峰值。 I queue roughly 20 jobs every second. 我每秒排队大约20个工作岗位。 Each job is roughly identical in terms of complexity. 每项工作在复杂性方面大致相同。 95% of the jobs spend less than 0.01ms in the call to QueueUserWorkItem. 在调用QueueUserWorkItem时,95%的作业花费不到0.01ms。 But every few seconds, seemingly at random, a job will take from 20-60ms! 但是,每隔几秒钟,似乎是随机的,一份工作将需要20-60毫秒!

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 将lock(obj)放在对job.queued变量的所有访问周围
  • Using SmartThreadPool instead of defaultThreadPool. 使用SmartThreadPool而不是defaultThreadPool。 Problem still persists inside STP's QueueWorkItem() 问题仍然存在于STP的QueueWorkItem()中

运行mono时尝试使用--server标志(仅与mono master兼容,因为它最近提交)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM