Mono ThreadPool并发问题 [英] Mono ThreadPool concurrency issues

查看:493
本文介绍了Mono ThreadPool并发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个使用ThreadPool进行多线程处理的软件.

I wrote one software that uses ThreadPool for multithreading.

ThreadPool.SetMinThreads(128, 128);
ThreadPool.SetMaxThreads(512, 512);

for (int i = 0; i < 40; i++)
{
    ThreadPool.QueueUserWorkItem(_ =>
    {
        Console.Write("!");
        Thread.Sleep(1000);
        Console.Write(".");
    }, null);
}

在每个线程中,我执行阻止网络操作(使用http). 围绕阻塞网络模型设计的软件,我无法迁移到非阻塞1线程I/O.

Inside each thread I perform blocking network operations(work with http). Software designed around blocking network model and I cannot move to non blocking 1 threaded I/O.

它在Windows平台上运行完美,我可以在每个内核上使用128-512个线程,而不会出现任何问题,所有工作都应该正常进行. 但是当我转向单声道时,我看到了一些非常奇怪的行为.我不能让一个CPU内核单声道运行多个线程,最多可以-每个内核1个线程,与我在SetMinThreads/SetMaxThreads中指定的内容无关.

It works perfect on windows platform, I can use 128-512 threads per one core without any issues, all work as it should work. But when I moved to mono, I saw some really strange behaviour. I cannot make mono run many threads per one CPU core, max I can get - 1 thread per core, doesn't matter what I do specify in SetMinThreads/SetMaxThreads.

在具有.NET 4/4.5,MONO版本3.2.1和我以前的系统上的某些旧版本的Linux下尝试. 顺便说一句,普通线程代码可以很好地工作,例如,这样可以得到预期的结果:

tried under Linux with .NET 4/4.5, MONO version 3.2.1 and some older version on my previous system. Btw, plain threading code works well, for example this gives desired result:

for (int i = 0; i < 20; i++)
{
   var t = new Thread(_ => {
      Console.Write("!");
      Thread.Sleep(1000);
      Console.Write(".");
   });
   t.Start();
}

推荐答案

我们还对mono进行了很多试验,并发现以下帮助:

We've also experimented alot with mono and looks that following helps:

  1. 将环境变量MONO_THREADS_PER_CPU设置为更高的值.例如 导出MONO_THREADS_PER_CPU = 300(Linux).我不确定,但看起来如果没有设置每个cpu的更多线程",就无法通过setmin/max线程来调整线程池.
  2. 使用--server开关运行mono,但从3.2.3开始有效.此处说明 http://www.mono-project.com/Release_Notes_Mono_3.2 ,但此标志可能仅在启动的线程不足时才对启动有所帮助.
  1. setup environment variable MONO_THREADS_PER_CPU to higher values. for example export MONO_THREADS_PER_CPU=300 (linux). I'am not sure but looks that you can't tweak threadpool by setmin/max threads without "more threads per cpu" set.
  2. run mono with --server switch, but it works starting from 3.2.3. here explanation http://www.mono-project.com/Release_Notes_Mono_3.2 , but may be this flag helps at start only when not enough threads fired.

我们在linux上同时使用了选项和mono,运行速度非常快(与Windows上的.net相当)

We're using both options and mono on linux acting quite fast( comparable to .net on windows )

这篇关于Mono ThreadPool并发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆