Help with Mutex

B

Brian Hampson

I'm new to getting multi-threading working in C#, so I'm looking for
some help with Mutexes.


I have code that looks like the following:



Mutex MyMutex = new Mutex();

Then later in parts that I want to make sure work at separate times:

void Part1()
{
MyMutex.WaitOne();
{//Do Stuff}
MyMutex.ReleaseMutex();
}

void Part2()
{
myTimer.Stop();
MyMutex.WaitOne();
{//Do other stuff that must be not at the saame time as Part1}
MyMutex.ReleaseMutex();
myTimer.Start();
}

Should this work? Part1 is triggered via a FileSystemWatcher and Part2
is triggered via a timer tick.
 
T

Tom Spink

Brian said:
I'm new to getting multi-threading working in C#, so I'm looking for
some help with Mutexes.


I have code that looks like the following:



Mutex MyMutex = new Mutex();

Then later in parts that I want to make sure work at separate times:

void Part1()
{
MyMutex.WaitOne();
{//Do Stuff}
MyMutex.ReleaseMutex();
}

void Part2()
{
myTimer.Stop();
MyMutex.WaitOne();
{//Do other stuff that must be not at the saame time as Part1}
MyMutex.ReleaseMutex();
myTimer.Start();
}

Should this work? Part1 is triggered via a FileSystemWatcher and Part2
is triggered via a timer tick.

Hi Brian,

Possibly. But what happens if an error occurs somewhere, and your mutex
isn't released?

The best way, IMHO, to do this would be to use locking:

///
Object lockObject = new Object();

void Part1()
{
lock ( lockObject )
{
// Do Stuff
}
}

void Part2()
{
lock ( lockObject )
{
// Do Other Stuff
}
}
///
 
B

Brian Hampson

Thanks,

I'll give it a try. I'm still not sure why my Mutex code wasn't
working as hoped, I had try/catch/finally inside those mutex locks.
Meh. I'll see about the lock.

Brian Hampson
System Administrator
ALS Laboratory Group, North America
 
J

Jon Skeet [C# MVP]

Brian Hampson said:
I'm new to getting multi-threading working in C#, so I'm looking for
some help with Mutexes.

I have code that looks like the following:

Code "like" what's pasted is rarely useful - it's easy to miss the bugs
out at the same time as stripping other things (like try/finally).

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(I agree with Brian though - using lock is a lot better.)
 
B

Brian Hampson

Well, I've gone to the lock for now, and when I was stripping all the
comments out of the original function, I found that I had a return that
wouldn't release the mutex in certain circumstances. Doh!

A Homer moment :(

In THEORY, however (if I hadn't had that leaking MUTEX issue)... was my
concept of Mutex implementation sound?

Brian Hampson
 
J

Jon Skeet [C# MVP]

Brian Hampson said:
Well, I've gone to the lock for now, and when I was stripping all the
comments out of the original function, I found that I had a return that
wouldn't release the mutex in certain circumstances. Doh!

A Homer moment :(

In THEORY, however (if I hadn't had that leaking MUTEX issue)... was my
concept of Mutex implementation sound?

Well, the theory of "wait for the mutex, then release it" is sound - so
long as you don't have multiple mutexes and obtain them in different
orders, etc.
 

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