# Joystick configuration changes and threading

**URL:** <https://discourse.glfw.org/t/joystick-configuration-changes-and-threading/861>\
**Category:** support\
**Created:** [February 24, 2017, 2:54pm UTC](https://discourse.glfw.org/t/joystick-configuration-changes-and-threading/861 "2017-02-24T14:54:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![enci](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/enci/32/89_2.png) [@enci](https://discourse.glfw.org/u/enci)\
**Post date:** [February 24, 2017, 2:54pm UTC](https://discourse.glfw.org/t/joystick-configuration-changes-and-threading/861/1 "2017-02-24T14:54:37Z")

</div>

Are the callbacks registered with glfwSetJoystickCallback guaranteed to be called in the main thread? I would expect to receive them when calling glfwPollEvents, but I am getting some weird behavior…

---

<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:** [February 24, 2017, 3:15pm UTC](https://discourse.glfw.org/t/joystick-configuration-changes-and-threading/861/2 "2017-02-24T15:15:08Z")

</div>

Note that GLFW requires that you perform window creation and glfwPollEvents from the main thread for compatibility reasons - [see this answer to a previous question about GLFW](http://discourse.glfw.org/t/multithreading-glfw/573/5).

If you’re following that advice, then all callbacks should occur on the main thread.

---

<div class="post-metadata">

**Author:** ![enci](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/enci/32/89_2.png) [@enci](https://discourse.glfw.org/u/enci)\
**Post date:** [February 24, 2017, 4:15pm UTC](https://discourse.glfw.org/t/joystick-configuration-changes-and-threading/861/3 "2017-02-24T16:15:19Z")

</div>

Thanks for that info. I am following advice and the callback are indeed all coming from the main thread. Could it be that glfwGetJoystickAxes is triggering a callback?  
My joystick state collection gets invalidated while looping over it (if the joystick gets unplugged) and there is nothing by glfwGetJoystickAxes and glfwGetJoystickButtons in the loop.

---

<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:** [February 24, 2017, 5:35pm UTC](https://discourse.glfw.org/t/joystick-configuration-changes-and-threading/861/4 "2017-02-24T17:35:22Z")

</div>

glfwGetJoystickAxescan can trigger a callback if the joystick was removed. This callback will occur on the same thread as glfwGetJoystickAxeswas called, which should be the main thread as glfwGetJoystickAxes should only be called from that thread.

If the joystick you’re polling is disconnected you should also get NULL back from glfwGetJoystickAxes.

---

<div class="post-metadata">

**Author:** ![kundelstein](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@kundelstein](https://discourse.glfw.org/u/kundelstein)\
**Post date:** [August 6, 2017, 9:21am UTC](https://discourse.glfw.org/t/joystick-configuration-changes-and-threading/861/5 "2017-08-06T09:21:04Z")

</div>

I have just stumbled across this thread while trying to solve similar issue. I’m keeping the list of “valid” joysticks, removing or adding to the list through the callbacks. However, sometimes my list gets invalidated as if the callback has been launched in parallel to the code.
