Skip to content

Instantly share code, notes, and snippets.

@Const-me Const-me/SyncTest.cs
Created May 25, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.