Threads and SDL_PollEvent

Hey!

I was coding away yesterday and made a multithreaded OpenGL program using SDL.
I set up two threads, the thread where main() is called and a extra thread for
input. The program functions well, it’s drawing the 3D stuff I’m expecting and
it receives key events as it should. But the window is not responding to
anything, I cant move, minimize, close or resize it (I’m not sure I should be
able to resize it though). My event polling loop looks (aproximately) like this

while(1){
SDL_Event event;
while( SDL_PollEvent(&event) ){
switch(event.type){
case SDL_KEY:
// handle keystuff and break;
case SDL_QUIT:
// Handle quit event by exit()ing program.
}
}

Scavenging archives, documentation and testing some examples I found out that
if I move the event loop to the main() thread (the thread that called SDL_Init
and SDL_SetVideoMode) then the window is moveable, minimizable and closaable.

So, now everything is tiptop, but as a computer scientist student I get curious
about the tecnical reason for this behaviour. In the docs I’ve read that
SDL_PollEvent should not be called from “separate” threads… but I’m not sure
what that means. Well, would be fun with an explanation.

Oh yeah, I’m currently coding using Visual Studio .NET under WinXP with SDL
1.2.6, haven’t tried it under Linux.

Cya Guys, Popi.

So, now everything is tiptop, but as a computer scientist student I get
curious
about the tecnical reason for this behaviour. In the docs I’ve read that
SDL_PollEvent should not be called from “separate” threads… but I’m not
sure
what that means. Well, would be fun with an explanation.

From the MSDN docs on GetMessage (one of the underlying windows functions
for event pumping):

If hWnd is NULL, GetMessage retrieves messages for any window that belongs
to the calling thread and thread messages posted to the calling thread via
PostThreadMessage. GetMessage does not retrieve messages for windows that
belong to other threads nor for threads other than the calling thread, even
if hWnd is not NULL.

Since SDL is just a wrapper on top of this, it exhibits the same behaviour

  • it will only get messages related to its own thread.

HTH,

Neil.

From the MSDN docs on GetMessage (one of the underlying windows functions
for event pumping):

If hWnd is NULL, GetMessage retrieves messages for any window that belongs
to the calling thread and thread messages posted to the calling thread via
PostThreadMessage. GetMessage does not retrieve messages for windows that
belong to other threads nor for threads other than the calling thread, even
if hWnd is not NULL.

Since SDL is just a wrapper on top of this, it exhibits the same behaviour

  • it will only get messages related to its own thread.

Ok, so basically, the thread that calls SDL_SetVideoMode, and therefore creates
the window, must pump the event queue in one way or another? And if I do it
from any other thread I mess it up for the SDL_SetVideoMode thread ? And the
behaviour might differ from platform to platform?

Thanks for such a swift response.

At 01:45 PM 02/10/2003 +0200, you wrote:

From the MSDN docs on GetMessage (one of the underlying windows functions
for event pumping):

If hWnd is NULL, GetMessage retrieves messages for any window that belongs
to the calling thread and thread messages posted to the calling thread via
PostThreadMessage. GetMessage does not retrieve messages for windows that
belong to other threads nor for threads other than the calling thread,
even
if hWnd is not NULL.

Since SDL is just a wrapper on top of this, it exhibits the same behaviour

  • it will only get messages related to its own thread.

Ok, so basically, the thread that calls SDL_SetVideoMode, and therefore
creates
the window, must pump the event queue in one way or another? And if I do it
from any other thread I mess it up for the SDL_SetVideoMode thread ? And the
behaviour might differ from platform to platform?

Thanks for such a swift response.

I’d say the behaviour certainly depends on the platform, but it looks like
the safest way is indeed to keep the pumping in the same thread as
SDL_SetVideoMode. I wouldn’t say that pumping in another thread messes it
up for the thread (on Windows at least!); it just won’t pick up any of the
relevant messages because it is in the “wrong” thread.

Neil.

From the MSDN docs on GetMessage (one of the underlying windows functions
for event pumping):

If hWnd is NULL, GetMessage retrieves messages for any window that belongs
to the calling thread and thread messages posted to the calling thread via
PostThreadMessage. GetMessage does not retrieve messages for windows that
belong to other threads nor for threads other than the calling thread, even
if hWnd is not NULL.

Since SDL is just a wrapper on top of this, it exhibits the same behaviour

  • it will only get messages related to its own thread.

Ok, so basically, the thread that calls SDL_SetVideoMode, and therefore creates
the window, must pump the event queue in one way or another?

Yes.

And if I do it
from any other thread I mess it up for the SDL_SetVideoMode thread ?

You just don’t get any events.

And the
behaviour might differ from platform to platform?

It is pretty much the same across platforms. This is actually covered in
the SDL documentation. Only the thread that calls setVideoMode can poll
for events.

	Bob PendletonOn Thu, 2003-10-02 at 06:45, Pontus Pihlgren wrote:

Thanks for such a swift response.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±----------------------------------+