The Computer Language
Benchmarks Game

thread-ring C# Mono LLVM program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
 * contributed by Isaac Gouy
 */

using System;
using System.Threading;


internal class NamedThread
{
   private int name;
   private AutoResetEvent signal = new AutoResetEvent(false);
   private int token = 0;

   internal NamedThread(int name) {
      this.name = name;
   }

   internal void Run() {
      while (TokenNotDone()) 
         NextThread().TakeToken(token-1);

      if (token == 0) Console.WriteLine(name);
      NextThread().TakeToken(-1);
   }

   private bool TokenNotDone() {
      signal.WaitOne();
      return token > 0;
   }

   internal NamedThread NextThread() {
      return ThreadRing.threadRing[ name % ThreadRing.numberOfThreads ];
   }

   internal void TakeToken(int x) {
      token = x;
      signal.Set();
   }
}


public class ThreadRing
{
   internal const int numberOfThreads = 503;
   internal static NamedThread[] threadRing = new NamedThread[503];

   public static void Main(string[] args) {
      for (int i = 0; i < numberOfThreads; i++){ 
         threadRing[i] = new NamedThread(i+1);
      }

      foreach (NamedThread t in threadRing) 
         new Thread(new ThreadStart(t.Run)).Start(); 

      threadRing[0].TakeToken( int.Parse(args[0]) );
   }
}
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Mono JIT compiler version 4.5.1 (master/3e844dd Fri May  6 19:24:07 PDT 2016)
	LLVM:          yes(3.6.0svn-mono-master/9f79399)
	GC:            sgen



Tue, 28 Jun 2016 20:46:15 GMT

MAKE:
mv threadring.csharpllvm threadring.cs
/usr/local/bin/mcs  -optimize+ -platform:x86 -out:threadring.csharpllvm_run threadring.cs
rm threadring.cs
0.25s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/mono --llvm --gc=sgen threadring.csharpllvm_run 50000000

PROGRAM OUTPUT:
292