I recently tried to write an example for lock statement. Consider following code:
public partial class Form1 : Form
{
private class Concurrency
{
private int _myValue;
private object _locker = new object();
public int Value
{
set
{
lock (_locker)
{
_myValue = value;
Thread.Sleep(new Random().Next(5, 25));
}
}
get
{
return _myValue;
}
}
}
private Random _random;
private Concurrency _concurrency;
public Form1()
{
InitializeComponent();
_random = new Random();
_concurrency = new Concurrency();
}
private void button1_Click(object sender, EventArgs e)
{
CreateTask(1);
CreateTask(2);
}
private void CreateTask(int taskId)
{
Task.Factory.StartNew(() =>
{
for (int i = 0; i < 10; ++i)
{
int randomNumber = _random.Next(0, 50);
Console.WriteLine("Thread {0}, setting value {1}", taskId, randomNumber);
_concurrency.Value = randomNumber;
Console.WriteLine("Thread {0}, getting value {1}", taskId, _concurrency.Value);
Thread.Sleep(_random.Next(5, 15));
}
});
}
}
The result is:
Thread 2, setting value 4
Thread 1, setting value 22
Thread 2, getting value 22
Thread 1, getting value 22
Thread 2, setting value 11
Thread 2, getting value 11
Thread 1, setting value 8
Thread 2, setting value 41
Thread 1, getting value 8
Thread 1, setting value 30
Thread 2, getting value 41
Thread 1, getting value 30
Thread 2, setting value 18
Thread 1, setting value 42
Thread 2, getting value 18
Thread 2, setting value 30
Thread 1, getting value 42
Thread 1, setting value 24
Thread 2, getting value 30
Thread 1, getting value 24
Thread 2, setting value 13
Thread 1, setting value 7
Thread 2, getting value 13
Thread 2, setting value 13
Thread 1, getting value 7
Thread 2, getting value 13
Thread 1, setting value 38
Thread 2, setting value 19
Thread 1, getting value 38
Thread 1, setting value 4
Thread 2, getting value 19
Thread 2, setting value 44
Thread 1, getting value 4
Thread 2, getting value 44
Thread 1, setting value 48
Thread 2, setting value 12
Thread 1, getting value 48
Thread 1, setting value 47
Thread 2, getting value 12
Thread 1, getting value 47
As you can see, everything is fine EXCEPT first setting/getting situation: Thread 2 sets value 2 but gets 22. And it's not single case, it happens every time. I know that setting and getting are not atomic and lock should be set around instructions in task, but why first attempt fails always and other work fine?
EDIT:
I updated Concurrency class to this:
private class Concurrency
{
private static Random _random = new Random();
private int _myValue;
private object _locker = new object();
public int Value
{
set
{
lock (_locker)
{
_myValue = value;
Thread.Sleep(_random.Next(5, 250));
}
}
get
{
return _myValue;
}
}
}
Note that I also expanded time range in Thread.Sleep. The result is:
Thread 2, setting value 3
Thread 1, setting value 9
Thread 2, getting value 9
Thread 2, setting value 44
Thread 1, getting value 9
Thread 1, setting value 35
Thread 2, getting value 44
Thread 2, setting value 32
Thread 1, getting value 35
Thread 1, setting value 25
Thread 2, getting value 32
Thread 2, setting value 15
Thread 1, getting value 25
Thread 1, setting value 5
Thread 2, getting value 15
Thread 2, setting value 34
Thread 1, getting value 5
Thread 1, setting value 42
Thread 2, getting value 34
Thread 2, setting value 36
Thread 1, getting value 42
Thread 1, setting value 8
Thread 2, getting value 36
Thread 2, setting value 42
Thread 1, getting value 8
Thread 1, setting value 16
Thread 2, getting value 42
Thread 2, setting value 0
Thread 1, getting value 16
Thread 1, setting value 43
Thread 2, getting value 0
Thread 2, setting value 20
Thread 1, getting value 43
Thread 1, setting value 30
Thread 2, getting value 20
Thread 2, setting value 38
Thread 1, getting value 30
Thread 1, setting value 0
Thread 2, getting value 38
Thread 1, getting value 0
Nothing changed really. I'm guessing that it's not matter of Random, but some other thing.