Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does SemaphoreSlim(1, 1) ensures read and writes are flushed from caches like lock?

Considering the following code snippet, does the _semaphore has the same effect as lock?

The outputs:

  1. using lock: 1000000
  2. _semaphore = new SemaphoreSlim(1, 1): 1000000 (I run the program 10 times and the results are the same)
  3. _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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading