title: GLFW & ImGui: Creating ImGui controls from thread other than main
body: <p>I am using GLFW and ImGui for a project that involves opening multiple windows. So far I have set this up so that each time a new window must be opened I spawn a thread that creates its own GLFW window and OpenGL context. The thread function looks something like this:</p>
  
  <pre class="lang-c++ prettyprint-override"><code>window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  // Check for creation error...
  glfwMakeContextCurrent(window);
  
  ImGui::CreateContext();
  ImGuiIO&amp; io = ImGui::GetIO();   // Is this supposed to be done per-thread?
  // Calling specific impl-specific ImGui setup methods for GLFW &amp; OpenGL3...
  // Set up OpenGL stuff ...
  
  while (!glfwWindowShouldClose(window))
  {
      // Some heavy-duty processing happens here...
  
      ImGui_ImplOpenGL3_NewFrame();
      ImGui_ImplGlfw_NewFrame();
      ImGui::NewFrame();
  
      // ImGui code is here...
  
      // Rendering some stuff in the window here...
  
      // Render ImGui last...
      ImGui::Render();
      ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  
      glfwSwapBuffers(window);
  }
  
  // Calling impl-specific ImGui shutdown here...
  glfwDestroyWindow(window);
  </code></pre>
  
  <p>I know that GLFW requires you to poll events from the main thread (the one that called <code>glfwInit()</code>), so I have a loop on my main thread that does that:</p>
  
  <pre class="lang-c++ prettyprint-override"><code>while (!appMustExit)
  {
      glfwWaitEvents();
  }
  // appMustExit is set from another thread that waits for console input
  </code></pre>
  
  <p>So the issue I am having is that my ImGui controls don't respond to any sort of input and <code>glfwWindowShouldClose()</code> never returns true if I click on the Close button. It seems like the input state is only available on the thread that calls <code>glfwPollEvents()</code>, which leads me to believe that you can't combine ImGui &amp; GLFW while still using a separate thread for rendering!</p>
  
  <p><strong>How can I fix this to allow ImGui &amp; these windows to respond to GLFW events?</strong></p>
  
  <p>My previous attempt used a single thread to iterate over each window and update/render it, but I am hoping to use threads to help the application scale better with many windows open.</p>
  
  <p><strong>Update:</strong> I would like to clarify that this application involves processing complex machine vision in real-time, and the ImGui code section is heavily integrated with controlling and responding to this machine vision code. Therefore I would like to be able to call the ImGui functions on the same thread as this processing, which also means this thread must be able to respond to glfw input.</p>