동기화 테스트 예제
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SyncTest1
{
class Sync
{
int m_count = 0;
public int Count
{
get { return m_count; }
}
public void Count100()
{
for (int i = 0; i < 100; i++)
{
//Monitor.Enter(this);
//try
//{
lock(this)
{
int temp = m_count;
Thread.Sleep(1);
temp++;
//Interlocked.Increment(ref temp);
m_count = temp;
}
//}
//finally
//{
// Monitor.Exit(this);
//}
}
}
}
class Program
{
static void Main(string[] args)
{
Sync sync = new Sync();
Thread syncThread = new Thread(new ThreadStart(sync.Count100));
Thread syncThread2 = new Thread(new ThreadStart(sync.Count100));
syncThread.Start();
syncThread2.Start();
syncThread.Join();
syncThread2.Join();
Console.WriteLine("{0}까지 셌습니다",sync.Count);
}
}
}