# Destroying multiple windows individually

**URL:** <https://discourse.glfw.org/t/destroying-multiple-windows-individually/938>\
**Category:** support\
**Created:** [July 8, 2017, 5:13pm UTC](https://discourse.glfw.org/t/destroying-multiple-windows-individually/938 "2017-07-08T17:13:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Numbnut](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/numbnut/32/122_2.png) [@Numbnut](https://discourse.glfw.org/u/Numbnut)\
**Post date:** [July 8, 2017, 5:13pm UTC](https://discourse.glfw.org/t/destroying-multiple-windows-individually/938/1 "2017-07-08T17:13:14Z")

</div>

Hello! today I was working on my python opengl library, and I switched to GLFW because of it’s promising simplicity and quality.  
Right now a single window works fine, but I have a problem with destroying windows when there is more than one. Each window’s loop is running in a separate thread, because i dont want to call makeContextCurrent each frame, so we’ve got 2 threads.  
The loops are identical, each executing this order:  
`render() -> swapBuffers(window) -> pollEvents()`

Each window’s main loop code:

> **Code**
>
> `while not glfw.window_should_close(self.window){ render() glfw.swap_buffers(self.window) glfw.poll_events() } glfw.destroy_window(self.window) print("destroyed")`

Now to the voodoo part:  
When i press exit on my main thread’s window it closes, and the other window stops reacting to events, forcing me to terminate the process.  
when i press exit in my second window, it calls the destroy function (“destroyed” is printed) but nothing happens! and then if i press it again, the window closes properly.

A: What could be the possible explanation for having to click exit twice?  
B: Why pollEvents is not window specific? polling from both threads doesnt seem to be the problem because i tested it both ways, and the results were the same. Maybe i don’t understand something about the event system which makes me voodoofied?

I’d be eternally grateful if someone helps me out with this 🙂  
I simply want to make an independant black window class

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 8, 2017, 5:38pm UTC](https://discourse.glfw.org/t/destroying-multiple-windows-individually/938/2 "2017-07-08T17:38:58Z")

</div>

As per the documentation on [glfwPollEvents](http://www.glfw.org/docs/latest/group__window.html#ga37bd57223967b4211d60ca1a0bf3c832) this function should only be called on the main application thread. You can continue to do all rendering on the separate window threads. For a little more explication see the answer by @elmindreda [on this thread](http://discourse.glfw.org/t/multithreading-glfw/573/5).

So try this approach of a single poll function in the main thread, and if you continue to have issues let us know.

---

<div class="post-metadata">

**Author:** ![Numbnut](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/numbnut/32/122_2.png) [@Numbnut](https://discourse.glfw.org/u/Numbnut)\
**Post date:** [July 8, 2017, 7:26pm UTC](https://discourse.glfw.org/t/destroying-multiple-windows-individually/938/3 "2017-07-08T19:26:58Z")

</div>

Thanks for the tip, I missed that part in the docs.  
Anyway, I did try calling pollEvents only from the main thread. It did not change anything, but as a good practice i’m gonna keep doing that. I still get that problem when I destroy my separate thread window, and it closes only after I press exit the second time (after it has already been destroyed)

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 9, 2017, 9:47am UTC](https://discourse.glfw.org/t/destroying-multiple-windows-individually/938/4 "2017-07-09T09:47:41Z")

</div>

This is a fairly complicated problem and without access to the entire source code it’s difficult to help you debug your problem, but here’s some thoughts:

You need to keep polling events until you decided to close ALL windows. One architecture to achieve this would be to poll events on the main thread, and then render each window on separate threads. Note that window creation and destruction need to be called from the main thread, then you can pass the window handles to your render threads. The C function [glfwWindowShouldClose](http://www.glfw.org/docs/latest/group__window.html#ga24e02fbfefbb81fc45320989f8140ab5) can be called from any thread, and I guess that this is also true for the python `glfw.window_should_close()` function. Note that with this approach you may want to change the event handling call from poll\_events() to [glfwWaitEvents](http://www.glfw.org/docs/latest/group__window.html#ga554e37d781f0a997656c26b2c56c835e) otherwise your main loop may spin using up CPU cycles needlessly.

Polling events is not window specific for OS specific reasons, and because many events are not window specific themselves. However the events themselves can be window specific, and glfw passes the window handle to any callback you’ve set so that you can take the relevant action.

---

<div class="post-metadata">

**Author:** ![Numbnut](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/numbnut/32/122_2.png) [@Numbnut](https://discourse.glfw.org/u/Numbnut)\
**Post date:** [July 9, 2017, 8:35pm UTC](https://discourse.glfw.org/t/destroying-multiple-windows-individually/938/5 "2017-07-09T20:35:35Z")

</div>

Up until now I’ve been destroying each window in it’s respective thread, and looking at your answer I’m now testing the other way of managing all the events at the main thread. And then I can feed the events into a message queue for each [window.to](http://window.to) read.  
Trying it now, It worked! 🙂  
Thank you greatly.  
Do you know if there’s any difference between running my window in the main thread vs sub-thread, like I do now? like, at the window level.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 9, 2017, 8:49pm UTC](https://discourse.glfw.org/t/destroying-multiple-windows-individually/938/6 "2017-07-09T20:49:17Z")

</div>

Excellent, glad that works.

It’s a common pattern to have a main thread processing events, and a thread per window to handle rendering - so if this is what you’re now doing that sounds like the correct approach to continue with for OpenGL.
