469,927 Members | 1,237 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

threading & mutex: question about example

Hi all,

I've been trying to get my head around threading. Here's an example
from the book I'm reading:

/***************************/
Mutex m = null;
const string name = "xyz";

try
{
m = Mutex.OLpenExisting(name);
}
catch (WaitHandleCannotBeOpenedException)
{ }

if (m == null)
{
// POINT 'A'
m = new Mutex(true, name);
}
else
{
m.close();
return;
}
/*****************************/

This example can be used in the main method to prevent two instances
of the same application to be run.

Now, as a newbie to threading, it seems to me that theoretically two
threads might *both* get to "Point 'A'", correct?

Thanks, regards,
Jeroen
Jan 7 '08 #1
2 1944
Jeroen,

Yes, two threads might get to point 'A', but that's not really the
problem here. Because the Mutex is named, it is visible to other processes,
so you have to worry about another process opening the thread if it finds
out that it doesn't exist.

To be honest, this isn't the best way to open or create a Mutex which is
named, as you have this race condition.

It's a better idea to call the CreateMutex API function through the
P/Invoke layer, which will open or create the named mutex if it doesn't
exist. Then, with the handle returned from that function, you would set the
SafeWaitHandle property of a new Mutex instance to that handle and call the
appropriate methods.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeroen" <me******@gmail.comwrote in message
news:91**********************************@q39g2000 hsf.googlegroups.com...
Hi all,

I've been trying to get my head around threading. Here's an example
from the book I'm reading:

/***************************/
Mutex m = null;
const string name = "xyz";

try
{
m = Mutex.OLpenExisting(name);
}
catch (WaitHandleCannotBeOpenedException)
{ }

if (m == null)
{
// POINT 'A'
m = new Mutex(true, name);
}
else
{
m.close();
return;
}
/*****************************/

This example can be used in the main method to prevent two instances
of the same application to be run.

Now, as a newbie to threading, it seems to me that theoretically two
threads might *both* get to "Point 'A'", correct?

Thanks, regards,
Jeroen

Jan 7 '08 #2
Nicholas,

Thanks for the quick response. I understand your answer (for the
larger part), though I'm a bit confused on the term "P/Invoke layer".
However, that seemed a different topic altogether so I posted a new
message in the newsgroup here:

http://groups.google.nl/group/micros...3883d5cdd010e9

Thanks again for the mutex explanation.

Regards,
Jeroen
Jan 7 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bardo | last post: by
3 posts views Thread by Steve B. | last post: by
16 posts views Thread by One Handed Man \( OHM - Terry Burns \) | last post: by
17 posts views Thread by One Handed Man \( OHM - Terry Burns \) | last post: by
2 posts views Thread by WXS | last post: by
6 posts views Thread by MobileBoy36 | last post: by
9 posts views Thread by cgwalters | last post: by
126 posts views Thread by Dann Corbit | last post: by
7 posts views Thread by dmitrey | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.