问题描述
我编写了一款使用 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 个线程,没有任何问题,一切正常.但是当我转向单声道时,我看到了一些非常奇怪的行为.我不能让 mono 在每个 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.
在 Linux 下使用 .NET 4/4.5、MONO 版本 3.2.1 和我以前的系统上的一些旧版本进行了尝试.顺便说一句,纯线程代码效果很好,例如这给出了预期的结果:
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:
- 将环境变量 MONO_THREADS_PER_CPU 设置为更高的值.例如导出 MONO_THREADS_PER_CPU=300 (linux).我不确定,但如果没有设置每个 cpu 更多线程",您将无法通过 setmin/max 线程调整线程池.
- 使用 --server 开关运行单声道,但它从 3.2.3 开始工作.这里解释http://www.mono-project.com/Release_Notes_Mono_3.2,但只有在没有足够的线程被触发时,这个标志才可能在启动时有帮助.
- 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.
- 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 上同时使用 options 和 mono 的速度非常快(与 windows 上的 .net 相当)
We're using both options and mono on linux acting quite fast( comparable to .net on windows )
这篇关于Mono ThreadPool 并发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!