问题
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);
}
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.
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.
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();
}
回答1:
We've also experimented alot with mono and looks that following helps:
- 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.
We're using both options and mono on linux acting quite fast( comparable to .net on windows )
回答2:
Why don't you use a combination of these stuff (like: Asyn, MultiThreading, Parallel) if multi threading is not enough for your work !? I think these may be helpful for you : http://mono-for-android.1047100.n5.nabble.com/Task-Parallel-Framework-issues-td5711359.html stackoverflow.com/questions/5717059/implementation-of-task-in-monodroid http://blog.errorok.com/2012/08/09/268/
来源:https://stackoverflow.com/questions/21512638/mono-threadpool-concurrency-issues