SDL mutex

Started by
6 comments, last by Madster 19 years, 5 months ago
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.
Working on a fully self-funded project
Advertisement
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.
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?
Working on a fully self-funded project
Try posting a minimal, but complete code sample that replicates your problem. Without code all we'd be able to supply is random guesses.
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?
Working on a fully self-funded project
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
Working on a fully self-funded project
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.
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.
Working on a fully self-funded project

This topic is closed to new replies.

Advertisement