# Threads and SDL\_PollEvent

**URL:** <https://discourse.libsdl.org/t/threads-and-sdl-pollevent/9452>\
**Category:** SDL Development\
**Created:** [October 2, 2003, 4:14am UTC](https://discourse.libsdl.org/t/threads-and-sdl-pollevent/9452 "2003-10-02T04:14:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pontus\_Pihlgren](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@Pontus\_Pihlgren](https://discourse.libsdl.org/u/Pontus_Pihlgren)\
**Post date:** [October 2, 2003, 4:14am UTC](https://discourse.libsdl.org/t/threads-and-sdl-pollevent/9452/1 "2003-10-02T04:14:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Neil\_Brown](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/n/848f3c/32.png) [@Neil\_Brown](https://discourse.libsdl.org/u/Neil_Brown)\
**Post date:** [October 2, 2003, 4:33am UTC](https://discourse.libsdl.org/t/threads-and-sdl-pollevent/9452/2 "2003-10-02T04:33:01Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Pontus\_Pihlgren](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@Pontus\_Pihlgren](https://discourse.libsdl.org/u/Pontus_Pihlgren)\
**Post date:** [October 2, 2003, 4:56am UTC](https://discourse.libsdl.org/t/threads-and-sdl-pollevent/9452/3 "2003-10-02T04:56:01Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Neil\_Brown](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/n/848f3c/32.png) [@Neil\_Brown](https://discourse.libsdl.org/u/Neil_Brown)\
**Post date:** [October 2, 2003, 5:24am UTC](https://discourse.libsdl.org/t/threads-and-sdl-pollevent/9452/4 "2003-10-02T05:24:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bob\_Pendleton](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@Bob\_Pendleton](https://discourse.libsdl.org/u/Bob_Pendleton)\
**Post date:** [October 2, 2003, 1:34pm UTC](https://discourse.libsdl.org/t/threads-and-sdl-pollevent/9452/5 "2003-10-02T13:34:00Z")

</div>

> > 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://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> –  
> ±----------------------------------+

- Bob Pendleton: independent writer +
- and programmer. +
- email: Bob at [Pendleton.com](http://Pendleton.com) +
- web: [www.GameProgrammer.com](http://www.GameProgrammer.com) +  
±----------------------------------+
