[SDL] Re: Using separate threads...

Stephane Peter megastep at lokigames.com
Thu Apr 27 21:04:10 PDT 2000


In article <slrn8gghg0.153.nop at compiler.linux.es>,
	nop at todolinux.org (Santiago Romero) writes:
> 
>  Hi! I'm using a separate thread and into it I do something like:
> 
> int quit_thread;
> int *keys;
> 
> int thread_func(void *unused)
> {
>    while ( quit_thread == 0 )
>    {
>         SDL_PumpEvents();
>         keys = SDL_GetKeyState(NULL);
>         if( keys[something] ) moresomething=1;
>         SDL_Delay(20);
>    }
>    return(0);
> }
> 
>  Then this thread will be executed about 50 times per second...
>  I use the keys[] in the main thread (the program itself) to
>  test if keys[something] == SDL_PRESSED, and somethings I wait
>  for SDL_KEYDOWN events (like a getch())... Into the thread
>  I'm using SDL_PumpEvents() and SDL_GetKeyState()... ¿Is this
>  thread usage secure? Can it hang, or this code is safe for
>  SDL to work? 
> 
>  I just want to have a 50Hz thread to sync other emulator
>  routines to some variable changes, and to allow the rest of
>  the emulator to access to keypresses using the *key array.
> 
>  I've tested SDL_SetTimer() but the emulator hangs sometimes!!!
>  It starts working, but something crashes (and sometimes not :),
>  and this thread-style works always...

That depends if you're doing some timer stuff from within your timer
handler, although the latest 1.1 version of SDL from CVS should be
safe in that respect. Just make sure you use the SDL threaded timers
and not the alarm() based ones that don't work properly in multithreaded
programs (call SDL_Init() with the SDL_INIT_EVENTTHREAD flag additionnally
to SDL_INIT_TIMER).

Are you using SDL 1.0 or 1.1 ? 

-- 
Stephane Peter
Programmer
Loki Entertainment Software

   "Microsoft has done to computers what McDonald's has done to gastronomy"



More information about the SDL mailing list