What is the scope of the 'lock' statement

G

Guest

I have a need to lock some objects at the process level (making sure multiple
threads do not share an object), and some objects at the machine level
(making sure multiple processess do not share an object).

I believe the scope of 'lock' is at the process level.
I believe the scope of 'mutex' is at the machine level.
It's nearly impossible to test my assumption, can anyone please confirm this
to be true?
 
G

Guest

I believe the scope of 'lock' is at the process level.
I believe the scope of 'mutex' is at the machine level.

You are correct. To use the Mutex across processes you need to give it a
name and use that name in all shared processes...
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

Technically, lock is at the app-domain level, since you can't pass an
object out of a domain (it is serialized out, or you have a proxy to it, and
the lock will work on one of those in it's own app domain).

Hope this helps.
 
J

Jon Skeet [C# MVP]

Brian Kitt said:
I have a need to lock some objects at the process level (making sure multiple
threads do not share an object), and some objects at the machine level
(making sure multiple processess do not share an object).

I believe the scope of 'lock' is at the process level.
I believe the scope of 'mutex' is at the machine level.
It's nearly impossible to test my assumption, can anyone please confirm this
to be true?

Yes, that's absolutely right. However, to get a Mutex to be at the
machine level instead of the user level, I believe you need to call it
a name beginning with "Global\".

See http://www.pobox.com/~skeet/csharp/threads/waithandles.shtml for
more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top