using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication6
{
class CountTest
{
private static object obj = new object();
private int count = 0;
public int Count
{
get { return count; }
set { count = value; }
}
public void Increment()
{
lock (obj)
{
count++;
}
}
public void Decrement()
{
lock (obj)
{
count--;
}
}
}
class Program
{
static void TestCounter(CountTest c)
{
for (int i = 0; i < 1000000; i++)
{
c.Increment();
c.Decrement();
}
}
static void Main(string[] args)
{
var tc = new CountTest();
var t1 = new Thread(() => TestCounter(tc));
var t2 = new Thread(() => TestCounter(tc));
var t3 = new Thread(() => TestCounter(tc));
var t4 = new Thread(() => TestCounter(tc));
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t1.Join();
t2.Join();
t3.Join();
t4.Join();
Console.WriteLine(" {0} ", tc.Count);
Console.Read();
}
}
}