Using separate threads

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…

CU and thx a lot.–
Windows 2000 no se cuelg?
AMIBIOS v2.8. 65535Kb OK. Iniciando Windows 2000…

-----------------------------------------------------

NoP / Compiler – nop @ todolinux.org
POWERED BY - Linux RedHat 6.0 - Reg. User #74.821
http://www.ctv.es/USERS/sromero

~-----------------------------------------------------~

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”

El 28 Apr 2000 04:04:10 GMT, Stephane Peter escribi?:

Hi! I’m using a separate thread and into it I do something like:
[…]
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 ?

1.0.8 I’m using it because is supposed being better to use the stable
version O:-) Well… I’ll tell you what I’m trying to do to see what
you advice me.

Imagine an emulator, something like:

int Z80Run(Numcycles)
{
while(!Numcycles–)
{
opcode=ReadMem(r_PC++);
switch(opcode)
{
#include “big_switch.c”
}
}
}

Then in the mainloop I have something like:

switch(debug)
{
case 0: Z80Run(224);
while(!timerwait) ;
timerwait–;
DisplayScanLine();
break;

And the timerwait is controlled by:

Uint32 threadfunc( void * )
{
while(!quit_thread)
{
timerwait++;
SDL_GetKeyState();
some_more_things_(NONE_GRAFICAL);
SDL_Delay(20);
}

}

Do you think I should use timers or theads for the above? Is just for
syncronise each scanline with the 50Hz timer, so that only an emulated
screen scanline is drawn at each 1/50 s.

thx a lot.

“Microsoft has done to computers what McDonald’s has done to gastronomy”

hehehe I’ll add it to my RANDSIG for Linux :)–
To hear the voice of God, type cat vmlinuz >/dev/audio

-----------------------------------------------------

NoP / Compiler – nop @ todolinux.org
POWERED BY - Linux RedHat 6.0 - Reg. User #74.821
http://www.ctv.es/USERS/sromero

~-----------------------------------------------------~