Jump to content
  • Advertisement
Sign in to follow this  
Jeroen Stout

Release a mutex owned by a different thread (Windows)

This topic is 4162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a small problem that I'd like to fix elegantly. I have written a mutex wrapper-class that allows multiple read-locks at the same time. It does this by locking an internal mutex the first time a read-lock is requested and unlocking it the moment the last read access is unlocked. However, the last unlock can be done by a different thread than the one doing the initial lock: My problem is that I end up with deadlocks, the thread that should unlock the mutex doesn't have the rights and fails to unlock it and the mutex is locked forever. I know ways around this but they involve Sleep's and I don't like too use too many of those. So my question is, can I have one thread release a mutex 'owned' by a different thread? ...hopefully I can :) thanks for your help

Share this post


Link to post
Share on other sites
Advertisement
Well I don't know exactly how you've implemented it all, but it might be easier if the mutex is owned by another object from which you obtained a wrapped mutexes via a static method (eg a factory which maps names to a mutex wrapper what returns a reference to the mutex wrapper). The wrapped mutex adds or removes references to the mutex in the map (via specific methods)... the method will (as appropriate) create/lock a mutex, or unlock it. I don't know if I've expained this well, or if this is a standard approach, but it's one I've used before.
Hope you get it working,

Dan

Share this post


Link to post
Share on other sites
Quote:
Original post by Jeroen Stout
I have written a mutex wrapper-class that allows multiple read-locks at the same time. It does this by locking an internal mutex the first time a read-lock is requested and unlocking it the moment the last read access is unlocked.


Are you counting the number of read locks? .. if so, are you counting in a thread-safe manner? :(


Share this post


Link to post
Share on other sites
You may not want to hear this, but if you want an elegant solution you might want to do some serious refactoring. What you are proposing sort of defeats the whole purpose of mutexes. :-)

Share this post


Link to post
Share on other sites
I was going to say use a semaphore instead, but it seems that there's no way (that I can see atm) to know what the count on a semaphore is using the Win32 API, which means you can't know if a lock is still being held by some thread, otherwise you could create a semaphore initialized to the max number of read locks you wanted to allow.

Maybe try implementing your own read locks using atomic ops?

Share this post


Link to post
Share on other sites
Firstly, a mutex is very probably overkill for your situation. A single-writer multiple-reader locking system can be created with a simple critical section. A semaphore would probably suit though.

Secondly, you have some serious design issues if you are wanting to release it from a different thread to that which you grabbed it from. All my experience tells me that it is very much a wrong thing to even want to do.

Can you show some code or give more details so we can show you the right way to proceed?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!