Rajanikant Hawaldar
Can we avoid deadlocks with the help of semaphore or what is the best way?

Can we avoid deadlocks with the help of semaphore or what is the best way?

By Rajanikant Hawaldar in C# on Jun 28 2021
  • Sonil  Kumar
    Jul, 2021 26

    Semaphore and Mutex both are interprocess lock mechanism for C#. You can aquire a lock in one process and no other process can take that lock until released.

    Mutex is a single lock and if any one thread has taken it no other thread in any other process or same process can take it until released.
    static Mutex mutex = new Mutex();**

    Whereas Semaphore is a mutex with a specified number of locks. You can specify the the number of lock can be aquired.
    static Semaphore _pool = new Semaphore(0, 4);

    • 2
  • Bohdan Stupak
    Nov, 2021 13

    The only thing I can add to the excellent answer above is that the best way to avoid deadlocks is not to have shared state between threads

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS