tags:

views:

324

answers:

3

I have a message queue on a shared system. This queue is accessed by 2 processes which run on 2 other machines. I need to control access to this queue by the 2 processes. So I need a "network mutex". How can I achieve this?

I don't think this is supported out of the box in C#.NET but if I have missed something very obvious, do point me in the right direction. This question has been asked before but the solutions suggested involved a database. I dont have any database in question.

http://stackoverflow.com/questions/1682699/how-to-mutex-across-a-network

+3  A: 

You need a third process, which actually owns the queued items. The two workers each ask the third process for the next item, and it's this third process that polices access and acts as the lock.

Will
This seems like the way to go but we already have a lot of MSIs in the system and I would like to avoid adding one more. Thanks!
atlantis
Looking for something directly supported by Win itself.
atlantis
+1  A: 

Use MSMQ or other queuing system design for use across a network.

Win32 has no cross machine synchronisation primitives like a muxtex, you might be able to build something from file locking, but you would just be re-inventing MSMQ.

Richard
I was wondering if there is something I could pass to OpenNamedMutex() that would cause it to open a named mutex on a remote machine taking care of the underlying connection and stuff and of course assuming required access rights are in place.Something like this would solve my problem. Any idea if MSMQ has something that I could use? (@aaronls' comment above)
atlantis
@atlantis: There is nothing you can pass, Win32 mutxes are limited to the current session (or possibly -- with sufficiently privileged user -- the whole machine). There is no possibility of a greater scope.
Richard
@atlantis: MSMQ supports a queue of (serialised) objects that can be safely used (including distributed transactions) across multiple machines. It is directly supported in .NET (`system.messaging` namespace).
Richard
A: 

I think I found what I was looking for ... and it turns out to be a simple property setting!

http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.denysharedreceive%28VS.80%29.aspx

Should have explored this first!!

Jeff key has reported an issue with this and his experience: http://weblogs.asp.net/jkey/archive/2004/05/12/130922.aspx

Thank you all for the help.

atlantis