Sign in to follow this  
Madster

SDL mutex

Recommended Posts

I have a class that has a private SDL_mutex. A method locks the mutex at the beginning and unlocks it before returning. After locking it prints "locked successfuly" and after unlocking it prints "unlocked successfuly". (or failed it it fails). The question is: How is it possible i'm getting this? Locked successfuly Locked successfuly segfault (SDL parachute, etc etc) when a mutex lock fails, does the locking function wait until the mutex is avaiable or it just reports it? If this is the case, why not just use a flag? I'm new to multithread implementations, but not to the concept. First time using a mutex here. Thanks.

Share this post


Link to post
Share on other sites
When you use SDL_mutexP to lock a mutex, any further code calling SDL_mutexP will be blocked until the mutex is unlocked. This is specified in the SDL documentation.

As to what it causing the segfault, I have no idea. It could very possibly be other code other than the SDL mutex stuff.

Share this post


Link to post
Share on other sites
thanks for the reply.
Yes, the segfault is being caused by the non-thread safe code that i'm trying to wrap with a mutex, but for some reason the mutex gets locked twice without getting unlocked... how can that be?

Share this post


Link to post
Share on other sites
Try posting a minimal, but complete code sample that replicates your problem. Without code all we'd be able to supply is random guesses.

Share this post


Link to post
Share on other sites
Good idea. Here's a minimal sample of what puzzles me. I am possibly understanding it the wrong way but...

#include "iostream"
#include "SDL\SDL.h"

using namespace std;

SDL_mutex *proxy_mutex;

int main(int argc, char *argv[])
{
SDL_Surface *screen;
atexit(SDL_Quit);
proxy_mutex = SDL_CreateMutex();
if(SDL_mutexP(proxy_mutex)==-1)
{
cout << "lock failed" << endl;
exit(1);
}
cout << "lock succeeded" << endl;

if(SDL_mutexP(proxy_mutex)==-1)
{
cout << "lock failed" << endl;
exit(1);
}
cout << "lock succeeded" << endl;


if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
system("pause");
exit(1);
}



screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if ( screen == NULL )
{
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}


SDL_DestroyMutex(proxy_mutex);
return 0;
}


this gives out two successes locking the same mutex.
Isn't the second one supposed to fail?

Share this post


Link to post
Share on other sites
Even smaller sample:


#include "iostream"
#include "SDL\SDL.h"

using namespace std;

SDL_mutex *proxy_mutex;

int main(int argc, char *argv[])
{
SDL_Surface *screen;
atexit(SDL_Quit);
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

proxy_mutex = SDL_CreateMutex();
SDL_LockMutex(proxy_mutex);
SDL_LockMutex(proxy_mutex);
cout << "Passed?" << endl;

SDL_DestroyMutex(proxy_mutex);
return 0;
}


this should indefinitely suspend the main thread.
Instead i see the program close and cout.txt merrily containing the text "Passed?"

Any help would be greatly appreciated here

Share this post


Link to post
Share on other sites
My guess is that SDL (or your OS) has some built-in protection against deadlocking your thread like that. You should try placing the second lock in a seperate thread and see if it still fails.

Share this post


Link to post
Share on other sites
Yes, Gyrbo, thanks.. i just found out in a 2003 SDL mailing list email that this is the case.

SDL explicitly allows locking an already locked mutex multiple times, but only from the same thread.
I think this is a bad idea, but SDL developers probably know much better than me =)

Anyways, found the answer to this and my segfault, and it was due to an iterator geting invalidated by another non-thread safe call.
Overhaul time! thanks for the replies.

I'm happy again. Yay.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this