[SDL] Help ! SDL Event handling in threads..May be a bug

Yuki Yuki001 at gmail.com
Sat Nov 11 21:27:04 PST 2006


Hello .I want to use SDL event in a thread . My application seems to work  
when using SDL 1.2.8 .But when I replace the sdl.dll with a 1.2.11 sdl.dll . 
My application doesn't work correctly. My OS is winXP sp2 .My compiler is VC++ 
6.0 sp4 . thanks.
a sample application code 
#include <sdl.h>
#include <stdio.h>
#include <SDL_thread.h>

#pragma comment (lib,"SDL.lib")
#pragma comment (lib,"SDLmain.lib")

bool quit;
int SDLCALL thread1(void * params)
{
  SDL_Event event;
  while (!quit)
  {
    // 更新event队列
    SDL_PumpEvents();
    // 此函数是线程安全的。
    SDL_PeepEvents(&event,1,SDL_GETEVENT,0xffffffff);
    /*
    // 可以用来替代 PumpEvents + PeepEvents
    // 此函数可以工作。但文档里并未提及线程安全性 
    SDL_PollEvent(&event);
    */
    if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_z)
    {
        printf ("z pressed !! in th1 \n");
    }
    if (event.key.keysym.sym == SDLK_ESCAPE)
        quit = true;
  }
  return 0;
}

int main (int argc, char * argv[])
{
  if (SDL_Init(SDL_INIT_VIDEO) < 0)
  {
    printf("Init Error !!\n");
    return 1;
  }
  SDL_Surface * screen = SDL_SetVideoMode(320,240,24, SDL_ANYFORMAT);
  if (screen == NULL)
  {
    printf("Video Error !! \n");
    SDL_Quit();
    return 1;
  }
  SDL_Thread * th1 = SDL_CreateThread(thread1,0);
  SDL_WaitThread(th1, NULL);
  SDL_Quit();
  return 0;
}







More information about the SDL mailing list