The Computer Language
Benchmarks Game

thread-ring C# Mono LLVM #2 program

source code

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

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;

internal class NamedThread {
    private readonly int name;
    private readonly ManualResetEventSlim signal = new ManualResetEventSlim(false);
    private int token;

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

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

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

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

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

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

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

    private static void Main( String[] args ) {
        Main2( args );
    }

    public static void Main2( string[] args ) {
        for ( var i = 0; i < numberOfThreads; i++ ) threadRing[ i ] = new NamedThread( i + 1 );
        var thrs = threadRing.Select( a => new Thread( a.Run ) ).ToArray();
        foreach ( var thread in thrs ) thread.Start(); 
        var cnt = args.Length > 0 ? int.Parse( args[ 0 ] ) : 50000000;
        threadRing[ 0 ].TakeToken( cnt );
        foreach ( var thread in thrs ) thread.Join();
    }
}
    

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:52:13 GMT

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

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

PROGRAM OUTPUT:
292