Skip to content

Instantly share code, notes, and snippets.

@Const-me
Created May 25, 2018 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Const-me/27bc9607bc844a59ec3e8dd9e4b902bc to your computer and use it in GitHub Desktop.
Save Const-me/27bc9607bc844a59ec3e8dd9e4b902bc to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadsTest
{
static class Program
{
static readonly object syncRoot = new object();
static readonly int[] array = new int[] { 1,2,3,4,5,6 };
static int i = 0;
static void thread()
{
lock( syncRoot )
{
for( ; i < array.Length; i++ )
{
Monitor.Pulse( syncRoot );
Monitor.Wait( syncRoot );
Console.WriteLine( "Thread {0}: {1}", Environment.CurrentManagedThreadId, array[ i ] );
}
}
}
static void Main( string[] args )
{
Action act = thread;
Task.WhenAll( Task.Run( act ), Task.Run( act ) ).Wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment