Forcibly destroy a mutex.

U

UJ

Is there any way to forcibly destroy a mutex?

Let's suppose I have a log file that I'm writing to. I have code in the
write that creates a mutex. But some program somewhere hasn't released it
yet. So after waiting a while (5 seconds let's say) I write to the file
anyway. (I figure it can't have taken that long.)

Problem is, the next time I try to write to the log file, I have to wait
that same 5 seconds because the mutex is still around. But I don't want
everybody waiting for it.

Is there any way to delete the mutex when you don't own it?

TIA - Jeff.
 
G

Guest

Hi,
Are you using System.Threading.Mutex ?

If you have mutexes around several process you can continue with the
execution using WaitAny, therefore if you own of the threads that you try to
synchronize (that's the purpose of the mutex) and you have finish the mutex
will continue the execution as "WaitAny" will allow you to execute when at
least one of the subscribed mutexes has finished.

Maybe you meant monitors or reader/writer locks.
 
M

Markus Stoeger

UJ said:
Problem is, the next time I try to write to the log file, I have to wait
that same 5 seconds because the mutex is still around. But I don't want
everybody waiting for it.

Maybe your program could just remember that the mutex is invalid and
ignore it the next time it needs to write to the file? So each process
will only wait the 5 secs once.

Anyway, the only clean solution would be to fix the program that doesn't
release the mutex!
Is there any way to delete the mutex when you don't own it?

None that I know of without checking the docs.. :)

hth,
Max
 
W

Willem van Rumpt

UJ said:
Is there any way to forcibly destroy a mutex?

Let's suppose I have a log file that I'm writing to. I have code in the
write that creates a mutex. But some program somewhere hasn't released it
yet. So after waiting a while (5 seconds let's say) I write to the file
anyway. (I figure it can't have taken that long.)

What do you mean by "creates a mutex"? Instantiates a new mutex? Gains
ownership?

If the latter, there's nothing you can do. It's what mutexes are for,
ensure safe access to the resource without other threads intervening.

If the former, I can't see how a mutex would work at all.

If the amount of time to write your logmessage takes that long, you're
probably better of with a producer/consumer queue (and scrutinizing why
on earth it takes that long might also be in order). Queue any message
you want to log, and have a dedicated thread dequeue them to write them
to their destination.
 

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