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?
>Solution :
The parameters in the semaphoreslim specify how many threads can operate concurrently and what the initial count is. If you want to use semaphoreslim as the equivalent of the c# lock statement, then the number of supported threads must be one. When you initialize your sempahore with 10, you’re saying that up to 10 threads can execute at the same time, which is not the same thing as a lock.
Semaphore has nothing to do with flushing reads or writes, to that is entirely based on how you use the semaphore.
If you’re going to use it as a lock, you need to be sure that every time the semaphore is waited, it is also released, so use a try/finally block.