The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
How to check if a mutex is locked?
Starg


Joined: 28 Mar 2014
Posts: 22
Hi there,

Is there a way to check if a mutex is locked?

The only way I can see how to do it at the moment is to try and lock it. If it isn't locked I can then unlock it, and if it is locked I get a SDL_MUTEX_TIMEDOUT value back.

However, this seems like a clumsy solution.

Cheers,
Steve
How to check if a mutex is locked?
Alvin Beach
Guest

On 11/09/14 15:52, Starg wrote:
Quote:
Hi there,

Is there a way to check if a mutex is locked?

The only way I can see how to do it at the moment is to try and lock it. If it isn't locked I can
then unlock it, and if it is locked I get a SDL_MUTEX_TIMEDOUT value back.

However, this seems like a clumsy solution.

Cheers,
Steve

Is this what you are looking for?

https://wiki.libsdl.org/SDL_TryLockMutex


Cheers,

Alvin

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
How to check if a mutex is locked?
David Olofson
Guest

On Thu, Sep 11, 2014 at 8:52 PM, Starg wrote:
[...]
Quote:
The only way I can see how to do it at the moment is to try and lock it. If it isn't locked I can then unlock it, and if it is locked I get a SDL_MUTEX_TIMEDOUT value back.
[...]

What are you trying to achieve here? What are you planning on doing in
the case where the mutex is locked?

The problem with merely checking the status of a mutex is that you
essentially end up with useless information, because someone else
might lock that mutex the next moment. That's why you have
non-blocking "trylock" operations in most APIs! Generally, you'll want
to do something if you acquire the lock, in which case you want it
locked until you're done, or you want to do something else (try a
different queue, render some more audio, render another video frame,
...) before you try again.


--
//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.--- Games, examples, libraries, scripting, sound, music, graphics ---.
| http://consulting.olofson.net http://olofsonarcade.com |
'---------------------------------------------------------------------'
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Starg


Joined: 28 Mar 2014
Posts: 22
Right, i see what you mean.

Well, in this particular case, my game saves data on exit. This is done on a separate thread. I wanted the main thread to check if the mutex was unlocked so the game can quit.
How to check if a mutex is locked?
Jonny D


Joined: 12 Sep 2009
Posts: 932
I think what you'd want is to SDL_WaitThread() in your main thread to be sure that all other threads are done processing.  If other threads process in a continuous loop, use a mutexed variable or a message queue to tell the other thread to quit the loop and let the thread end.


Jonny D