Extra question
Does SemaphoreSlim(1, 1)
still ensures that I have the correct output 1000000, even if task1
and task2
run on 2 different cores?
Original question
Considering the following code snippet, does the _semaphore
has the same effect as lock
?
The outputs:
- using lock: 1000000
_semaphore = new SemaphoreSlim(1, 1)
: 1000000 (I run the program 10 times and the results are the same)_semaphore = new SemaphoreSlim(10, 10)
: varies from 999998 to 999990 to 1000000, etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Calculate
{
private int _val = 0;
private object _addLock = new object();
private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
public int Val
{
get
{
return _val;
}
}
public void IncreManyTimes()
{
for (int i = 0; i < 500000; ++i)
{
//lock(_addLock)
//{
// _val = _val + 1;
//}
_semaphore.Wait();
_val = _val + 1;
_semaphore.Release();
}
}
}
class Program
{
static void Main(string[] args)
{
Calculate calculate = new Calculate();
Task.Run(() =>
{
Task task1 = Task.Run(() =>
{
calculate.IncreManyTimes();
});
Task task2 = Task.Run(() =>
{
calculate.IncreManyTimes();
});
Task.WaitAll(task1, task2);
}).Wait();
Console.WriteLine(calculate.Val);
Console.ReadKey();
}
}
}
The related question: Does locking ensure reads and writes are flushed from caches? If so, how?
And my question could be described as: Does SemaphoreSlim(1, 1)
ensure reads and writes are flushed from caches?