Locks vs. Mutex

Everything else that doesn't fall into one of the other PB categories.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Locks vs. Mutex

Post 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
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Locks vs. Mutex

Post 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.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Locks vs. Mutex

Post 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.
"Have you tried turning it off and on again ?"
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Locks vs. Mutex

Post 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 :lol:
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Locks vs. Mutex

Post 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?
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Locks vs. Mutex

Post by idle »

If you have cross thread access to the map then you should use a mutex
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Locks vs. Mutex

Post 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.
User_Russian
Addict
Addict
Posts: 1630
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Locks vs. Mutex

Post 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.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Locks vs. Mutex

Post 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
"Have you tried turning it off and on again ?"
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Locks vs. Mutex

Post 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! :wink:
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply