title: OpenGL Rendering in a secondary thread
body: <p>I'm writing a 3D model viewer application as a hobby project, and also as a test platform to try out different rendering techniques. I'm using SDL to handle window management and events, and OpenGL for the 3D rendering. The first iteration of my program was single-threaded, and ran well  enough. However, I noticed that the single-threaded program caused the system to become very sluggish/laggy. My solution was to move all of the rendering code into a different thread, thereby freeing the main thread to handle events and prevent the app from becoming unresponsive. </p>
  
  <p>This solution worked intermittently, the program frequently crashed due to a changing (and to my mind bizarre) set of errors coming mainly from the X window system. This led me to question my initial assumption that as long as all of my OpenGL calls took place in the thread where the context was created, everything should still work out. After spending the better part of a day searching the internet for an answer, I am thoroughly stumped. </p>
  
  <p>More succinctly: Is it possible to perform 3D rendering using OpenGL in a thread other than the main thread? Can I still use a cross-platform windowing library such as SDL or GLFW with this configuration? Is there a better way to do what I'm trying to do?</p>
  
  <p>So far I've been developing on Linux (Ubuntu 11.04) using C++, although I am also comfortable with Java and Python if there is a solution that works better in those languages.</p>
  
  <p><strong>UPDATE:</strong> As requested, some clarifications:</p>
  
  <ul>
  <li>When I say "The system becomes sluggish" I mean interacting with the desktop (dragging windows, interacting with the panel, etc) becomes much slower than normal. Moving my application's window takes time on the order of seconds, and other interactions are just slow enough to be annoying.</li>
  <li>As for interference with a compositing window manager... I am using the GNOME shell that ships with Ubuntu 11.04 (staying away from Unity for now...) and I couldn't find any options to disable desktop effects such as there was in previous distributions. I assume this means I'm not using a compositing window manager...although I could be very wrong. </li>
  <li>I believe the "X errors" are server errors due to the error messages I'm getting at the terminal. More details below. </li>
  </ul>
  
  <p>The errors I get with the multi-threaded version of my app:</p>
  
  <blockquote>
    <p>XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
          after 73 requests (73 known processed) with 0 events remaining.</p>
    
    <p>X Error of failed request:  BadColor (invalid Colormap parameter)
      Major opcode of failed request:  79 (X_FreeColormap)
      Resource id in failed request:  0x4600001
      Serial number of failed request:  72
      Current serial number in output stream:  73</p>
    
    <p>Game: ../../src/xcb_io.c:140: dequeue_pending_request: Assertion `req == dpy->xcb->pending_requests' failed.
    Aborted</p>
  </blockquote>
  
  <p>I always get one of the three errors above, which one I get varies, apparently at random, which (to my eyes) would appear to confirm that my issue does in fact stem from my use of threads. Keep in mind that I'm learning as I go along, so there is a very good chance that in my ignorance I've something rather stupid along the way.</p>
  
  <p><strong>SOLUTION:</strong> For anyone who is having a similar issue, I solved my problem by moving my call to <code>SDL_Init(SDL_INIT_VIDEO)</code> to the rendering thread, and locking the context initialization using a mutex. This ensures that the context is created in the thread that will be using it, and it prevents the main loop from starting before initialization tasks have finished. A simplified outline of the startup procedure:</p>
  
  <p>1) Main thread initializes <code>struct</code> which will be shared between the two threads, and which contains a mutex.<br>
  2) Main thread spawns render thread and sleeps for a brief period (1-5ms), giving the render thread time to lock the mutex. After this pause, the main thread blocks while trying to lock the mutex.<br>
  3) Render thread locks mutex, initializes SDL's video subsystem and creates OpenGL context.<br>
  4) Render thread unlocks mutex and enters its "render loop".<br>
  5) The main thread is no longer blocked, so it locks and unlocks the mutex before finishing its initialization step.  </p>
  
  <p>Be sure and read the answers and comments, there is a lot of useful information there.  </p>