[SDL] (openGL repaint) thread problem

Johannes Schmidt sdl at myrealbox.com
Tue Aug 7 12:02:03 PDT 2001


Hi,

I have some problems using threads.

If I try to do some drawing stuff in a thread created with SDL_CreateThread, 
the application exits with
Fatal signal: Segmentation Fault (SDL Parachute Deployed)
Xlib: unexpected async reply (sequence 0x66)!

If I do the same drawing within the main thread, the application runs as 
expexted.

example code:
#include "SDL/SDL.h"
#include "SDL/SDL_thread.h"

#include "GL/gl.h"

bool quitThread;

int eventLoop(void * data) {
    SDL_Event event;

    while(!quitThread) {
        SDL_WaitEvent(&event);
            switch( event.type ) {
                case SDL_KEYDOWN:
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        quitThread = true;
                break;
                case SDL_QUIT:
                    quitThread = true;
                break;
                default:
                break;
            }
    }
    return -1;
}

int repaint(void * data) {
    while(!quitThread) {
        glClear( GL_COLOR_BUFFER_BIT );
        glLoadIdentity();

        glBegin( GL_QUADS );
        glVertex2i(10, 10);
        glVertex2i(10, 100);
        glVertex2i(100, 100);
        glVertex2i(100, 10);
        glEnd();

        SDL_GL_SwapBuffers();
    }
    return -1;
}

int main( int argc, char * argv[] ) {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_SetVideoMode( 320, 240, 32, SDL_OPENGL);

    glViewport(0, 0, 320, 240);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( 0, 320, 0, 240, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    quitThread = false;

    SDL_Thread * thr1;
    SDL_Thread * thr2;

    thr1 = SDL_CreateThread(eventLoop, NULL);

    // separate thread
    // comment this line to create a working application
    thr2 = SDL_CreateThread(repaint, NULL);

    // no separate thread, call function directly
    // uncomment this line to create a working application
    //repaint(NULL);

    SDL_WaitThread(thr1, NULL);

    return 0;
}


Regards,
Johannes




More information about the SDL mailing list