The Computer Language
Benchmarks Game

thread-ring C# .NET Core #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:
64-bit Ubuntu quad core
2.0.2 a04b4bf512
"System.GC.Server": true


Fri, 27 Oct 2017 01:39:30 GMT

MAKE:
cp threadring.csharpcore-2.csharpcore Program.cs
cp Include/csharpcore/tmp.csproj .
cp Include/csharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/csharpcore/tmp.csproj.nuget.g.props ./obj
cp Include/csharpcore/tmp.csproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/benchmarksgame_quadcore/threadring/tmp/bin/Release/netcoreapp2.0/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.52

6.12s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp2.0/tmp.dll 50000000

PROGRAM OUTPUT:
292