Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono ThreadPool concurrency issues

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();
}
like image 302
sfireman Avatar asked Feb 02 '14 15:02

sfireman


1 Answers

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

  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.

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

like image 199
Konstantin Sokharev Avatar answered Oct 16 '22 22:10

Konstantin Sokharev