SDL Threads: graphics and events in separate contexts?

Is it possible to run OpenGL render loop and event processing loop in
different threads (event in main, render in SDL_CreateThread-spawned one)?

I’m getting a bunch of X11 errors and a SIGSEGV in the end:
Xlib: sequence lost (0x10176 > 0x6a) in reply type 0x17!
Xlib: sequence lost (0x10000 > 0x6a) in reply type 0x1!
XIO: fatal IO error 0 (Success) on X server ":0.0"
after 106 requests (0 known processed) with 3 events remaining.
X Error of failed request: 64
Major opcode of failed request: 127 (X_NoOperation)
Serial number of failed request: 40049
Current serial number in output stream: 106–
Sincerely,
Vladimir “Farcaller” Pouzanov Hack&Dev Team
PGP/GPG: 0x3A40FF29 http://hackndev.com
Fingerprint: FA36 877A 2DC3 B56F CAB5 7DB3 4C97 A596 3A40 FF29
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: This is a digitally signed message part.
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080106/0d42038e/attachment.pgp

Graphics, in general, must be done from the thread main() is in. So
simply swap the tasks around: opengl in main() and event processing in
spawned one.On Jan 6, 2008 5:34 PM, Vladimir Pouzanov wrote:

Is it possible to run OpenGL render loop and event processing loop in
different threads (event in main, render in SDL_CreateThread-spawned one)?

I’ve already tried that with same result…On Sunday 06 January 2008 21:38:13 Brian wrote:

Graphics, in general, must be done from the thread main() is in. So
simply swap the tasks around: opengl in main() and event processing in
spawned one.


Sincerely,
Vladimir “Farcaller” Pouzanov Hack&Dev Team
PGP/GPG: 0x3A40FF29 http://hackndev.com
Fingerprint: FA36 877A 2DC3 B56F CAB5 7DB3 4C97 A596 3A40 FF29
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: This is a digitally signed message part.
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080106/2501f090/attachment.pgp

Well on Windows it appears to work:

#include “SDL.h”
#include “SDL_thread.h”

int inputThread( void *param )
{
volatile bool running = static_cast<volatile bool>(param);
SDL_Event event;
while(SDL_WaitEvent(&event))
{
if( event.type == SDL_QUIT)
{
*running = false;
return 0;
}
}
return 1;
}

int main(int, char**)
{
SDL_Init(SDL_INIT_VIDEO);

SDL_Surface *screen = SDL_SetVideoMode(800,600,32,SDL_OPENGL);

if( !screen )
{
    std::cerr << SDL_GetError() << std::endl;
    return 1;
}

volatile bool running = true;
SDL_Thread *thread = SDL_CreateThread(&inputThread,(void*)(&running));
while(running)
{
    glClearColor(rand() / static_cast<float>(RAND_MAX),0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_LINES);


        glVertex3f(6.0, 4.0, 2.0);
        glVertex3f(2.0, -4.0, 3.3);

        glVertex3f(5.0, 8.0, 8.0);
        glVertex3f(-4.7, 5.0, -3.0);

        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(6.0, -1.0, -7.0);

    glEnd();
    SDL_GL_SwapBuffers();

    SDL_PumpEvents();
}

SDL_WaitThread(thread,0);
return 0;

}

For thread safety you might need to use something like fast events
(http://gameprogrammer.com/fastevents/fastevents1.html). Though from
the docs I got the impression that the SDL event queue is supposed to
be thread safe.On Jan 6, 2008 7:42 PM, Vladimir Pouzanov wrote:

On Sunday 06 January 2008 21:38:13 Brian wrote:

Graphics, in general, must be done from the thread main() is in. So
simply swap the tasks around: opengl in main() and event processing in
spawned one.

I’ve already tried that with same result…


Sincerely,
Vladimir “Farcaller” Pouzanov Hack&Dev Team
PGP/GPG: 0x3A40FF29 http://hackndev.com
Fingerprint: FA36 877A 2DC3 B56F CAB5 7DB3 4C97 A596 3A40 FF29


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Note:

You can only call this function in the thread that set the video mode.

Just noticed this in SDL_PumpEvents. Now I know, why I can’t run event loop
and OpenGL in separate threads ;)On Sunday 06 January 2008 23:02:28 Brian wrote:

Well on Windows it appears to work:


Sincerely,
Vladimir “Farcaller” Pouzanov Hack&Dev Team
PGP/GPG: 0x3A40FF29 http://hackndev.com
Fingerprint: FA36 877A 2DC3 B56F CAB5 7DB3 4C97 A596 3A40 FF29
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: This is a digitally signed message part.
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080106/665a0e83/attachment.pgp