Page 1 of 1
Locks vs. Mutex
Posted: Sun Jul 21, 2013 3:17 am
by RichAlgeni
This is a very interesting article. I'd love to hear some feedback on this issue. The author references 'common language runtime', which I prefer to stay away from anyway!
http://blogs.microsoft.co.il/blogs/sash ... mutex.aspx
Re: Locks vs. Mutex
Posted: Sun Jul 21, 2013 11:49 pm
by idle
I think lock is the equivalent of a win32 critical section
the difference between a mutex vs critcal section is the scope Interprocess vs intraprocess
A critical section is a user owned lock to sync threads of the process, avoids context switches
A mutex is a kernel owned lock to provide interprocess synchronisation, requires context switches
I'm not sure what a PB Mutex is on windows without some digging.
Re: Locks vs. Mutex
Posted: Sun Jul 21, 2013 11:51 pm
by luis
idle wrote:
I'm not sure what a PB Mutex is on windows without some digging.
I think it's a critical section (not 100% sure). In fact I always found the name mutex confusing
I believe CLR implement the Lock natively by itself. Again not 100% sure.
Re: Locks vs. Mutex
Posted: Mon Jul 22, 2013 12:34 am
by idle
luis wrote:idle wrote:
I'm not sure what a PB Mutex is on windows without some digging.
I think it's a critical section (not 100% sure). In fact I always found the name mutex confusing
I believe CLR implement the Lock natively by itself. Again not 100% sure.
Yes it most likely is a Critical Section
and yes the CLR lock is a managed object from what I can tell and may even include monitoring to avoid deadlocks
A NumptyLock just what I need

Re: Locks vs. Mutex
Posted: Mon Jul 22, 2013 2:26 am
by RichAlgeni
The reason I was looking into this is that I am using a mutex to protect a map structure that contains file name, file length, expiration date, and a pointer to data. Since it's a structure in a map, I thought I should use a mutex, even when I am doing a 'FindMapElement'.
Is 'better safe than sorry' the correct course in this situation? Or should I look into using some for of a Lock?
Re: Locks vs. Mutex
Posted: Mon Jul 22, 2013 2:48 am
by idle
If you have cross thread access to the map then you should use a mutex
Re: Locks vs. Mutex
Posted: Mon Jul 22, 2013 3:17 am
by RichAlgeni
That's exactly what I am doing, using threads to access the map, with a Global mutex.
It seems to work great. So far I don't see any sort of resource hog issues occurring.
Re: Locks vs. Mutex
Posted: Mon Jul 22, 2013 9:16 am
by User_Russian
idle wrote:I'm not sure what a PB Mutex is on windows without some digging.
In Windows-version PB, is used, the critical section, not mutex.
But why is it called a mutex, is unclear.
Re: Locks vs. Mutex
Posted: Mon Jul 22, 2013 11:18 am
by luis
RichAlgeni wrote:That's exactly what I am doing, using threads to access the map, with a Global mutex.
It seems to work great. So far I don't see any sort of resource hog issues occurring.
If with mutex you mean a PB Mutex (CreateMutex()) is ok because actually it should be using a critical section and not an OS mutex, similarly to the desired course of action described in that article.
The problem is the PB object has been called Mutex, creating confusion with the CreateMutex_() API, and so when we talk about mutex here we should use the terms "PB Mutex" and "OS mutex" or we'll never be sure of what each other is talking about.
PB Mutex -> probably critical section -> threads only
OS mutex -> threads and different processes too (slower)
http://msdn.microsoft.com/en-us/library ... 85%29.aspx
Re: Locks vs. Mutex
Posted: Mon Jul 22, 2013 12:45 pm
by idle
User_Russian wrote:idle wrote:I'm not sure what a PB Mutex is on windows without some digging.
In Windows-version PB, is used, the critical section, not mutex.
But why is it called a mutex, is unclear.
They both perform mutual exclusion so they are both technically mutex's but yes it's confusing when you're used to win32 api's
Maybe critical section just sounded to scary!
