More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Friday, 27 May 2016 7:02:44 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 519

C# - Difference between lock, named mutext and unnamed mutex

lock: locks across threads in an AppDomain; wrapper on monitor; simpler leaner & faster. Cf. WinAPI CriticalSection.
mutex: performs cross-process locking and synchronization; operating system wide. cf. a lock is specific to the AppDomain.
unnamed mutex (local mutex): locks across AppDomain in a process, ie. more locking scope than lock.
Named mutex (system mutex): locks across process in an operating system, ie. more locking scope than unnamed mutex. Cf. A local mutex exists only within your process.

Unlike Monitor class, lock is not an actual class or object. It's just a compiler keyword. It's a wrapper around the functionality of the Monitor class and is designed to make the Monitor easier to work with. the reason that the lock / Monitor are restricted to the AppDomain is that a reference to a memory address (in the form of an instantiated object) is required to manage the "lock" and maintain the identity of the Monitor. Lock is a simplest way to protect section of your code. If synchronization across processes is not required, it is better to use lock (aka Monitor)˛ You can replace lock with Moniter if you want more controlled synchronization.

The Mutex is a .Net wrapper around an operating system construct, and can be used for system-wide synchronization, using string data (instead of a pointer to data) as its identifier. Mutex is kind of a key to use resources in the system. If you have mutex, you can use the resource, Otherwise you need to wait till the mutex is released. Process goes to wait queue for the resource. Note that Mutex is operating system wide and cannot be used between two remote processes.

Examples of using Mutex:
1. running only one instance of an application.
2. allowing only one process to access the same file.

Cf. ReaderWriterLock and ReaderWriterLockSlim in 3.5, Semaphore and the new SemaphoreSlim in .NET 4.0 - Slim classes cannot be used system wide.
Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.