• Top Posts

    Explain Lock, Monitors, and Mutex Object in Threading in C# ?

    • Lock keyword ensures that only one thread can enter a particular section of the code at any given time. In the above Example, lock(ObjA) means the lock is placed on ObjA until this process releases it, no other thread can access ObjA.
    • Mutex is also like a lock but it can work across multiple processes at a time. WaitOne() is used to lock and ReleaseMutex() is used to release the lock. But Mutex is slower than lock as it takes time to acquire and release it.
    • Monitor.Enter and Monitor.Exit implements lock internally. a lock is a shortcut for Monitors. lock(objA) internally calls.
    • EX:
                  Monitor.Enter(ObjA);
                  try{
                     }
                 Finally {Monitor.Exit(ObjA));}


    
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728