2
Reply

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?

    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);

    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