9

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();
}
9
  • 1
    Of course I can reinvent the wheel and write my own shiny ThreadPool that will behave well(based on last code snippet) But it seems so wrong... What is the reason of such mono behaviour?
    – sfireman
    Feb 2, 2014 at 16:17
  • You should use async http requests etc. This way you do not need many threads to handle requests.
    – Casperah
    Feb 2, 2014 at 16:21
  • 512 threads?! Do you realize how huge the overhead is? Why are you using so many threads, if you don't mind me asking?
    – dcastro
    Feb 2, 2014 at 16:23
  • Let me just point out that using tons of threads can be legitimate. If you've got an existing app, you don't rewrite it if avoidable. You turn up the thread dial. It is a very low-effort instrument to increase throughput. If that is enough to realize your goals, you're done in a minute.
    – usr
    Feb 2, 2014 at 16:36
  • That said, you are using thread pool auto scaling with IO bound tasks. That does not work because the thread pool does not know the right degree of parallelism. Your approach is very likely suboptimal by far.
    – usr
    Feb 2, 2014 at 16:37

2 Answers 2

7

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 )

5
  • Where is this option set? (MONO_THREADS_PER_CPU)
    – jjxtra
    Jul 1, 2014 at 22:59
  • 1
    Not sure what you mean, but it's environment variable, so you setting via "export" at linux and with "set" at windows. also you could check source code(search for MONO_THREADS_PER_CPU): github.com/mono/mono/blob/… Jul 2, 2014 at 19:45
  • 1
    This is the real deal - was having problems with Npgsql's async DNS name resolution, turns out it's because of a small threadpool on mono by default. Increased to (I know, too large, still tuning...) 2000 instead of the default 50, and voilà, no more problems with Npgsql or any other threadpool deadlock that I experienced earlier. May 4, 2016 at 7:06
  • We had been seeing what we though were random timeouts across SSH, HTTPS to multiple endpoints and DB access. This fixed our issues around timeouts soup to nuts. Thank you we are truly in your debt.
    – Gent
    Aug 24, 2016 at 21:03
  • Having done the above, we also had to set ThreadPool.SetMaxThreads high enough on a 4 CPU/core VM. When it was low we saw the timeouts, once we moved it up, things started working again. Only on 4 CPU/core VMs though.
    – AlG
    Jun 21, 2017 at 14:09
-1

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/

5
  • I do not target any mobile platforms. I tried this code in Linux inside Virtual Box. And no, it won't add my jobs to a signle thread. It must use min/max thread settings I gave using SetMinThread/SetMaxThread and queue extra tasks for later execution. As I told code works perfect under windows, but misbehave under mono/linux.
    – sfireman
    Feb 2, 2014 at 16:53
  • Oh! I didn't attention the first line. OK but what's your idea about combination of couple of techniques ? Feb 2, 2014 at 17:13
  • Parallel.For doesn't work either - the same problem, everything is single threaded, while on windows it's not problem to get up to 512 threads on one core. And I need to control max threads per one task, ThreadPool seemed me the simplest solution, but for some strange reason it's bugged on mono in all versions %)
    – sfireman
    Feb 2, 2014 at 17:18
  • You said you're testing it on Linux inside Virtual-Box. Are you sure that the CPU‌ properties you set, for your Linux virtual machine is ok ? Feb 2, 2014 at 17:46
  • There are no any special options. I've tried with 1 and 4 cpus. If I set 4 cpus I get 4 threads, if I set 1 I get 1 thread...
    – sfireman
    Feb 2, 2014 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.