title: SDL and X11 multithreading Fatal IO Error
body: <p>I'm trying to create a simple multithreading example using C++ and SDL as shown here:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "SDL/SDL_image.h"
  #include "SDL/SDL.h"
  #include "SDL/SDL_thread.h"
  #include "X11/Xlib.h"
  #include "SDLAbstractionLayer.h"
  #include &lt;string&gt;
  
  using namespace std;
  
  SDL_Thread* thread = NULL;
  bool quit = false;
  
  int myThread(void* data) {
     while (!quit) {
        //caption animation
        SDL_WM_SetCaption ("Thread is running", NULL);
        SDL_Delay(250);
  
        SDL_WM_SetCaption("Thread is running.", NULL);
        SDL_Delay(250);
  
        SDL_WM_SetCaption("Thread is running..", NULL);
        SDL_Delay(250);
  
        SDL_WM_SetCaption("Thread is running...", NULL);
        SDL_Delay(250);
     }
  
     return 0;
  }
  
  int main(int argc, char* argv[]) {
     SDL_Surface* screen = init(640, 480, "");
     XInitThreads();
  
     Surface test("resources/image.png");
     thread = SDL_CreateThread(myThread, NULL);
     SDL_Event event;
  
     while (!quit) {
        if(SDL_PollEvent(&amp;event)) {
           if (event.type == SDL_QUIT) {
              quit = true;
              break;
           }
  
           fillScreen(screen, Surface::WHITE);
           applySurface(0, 0, test, screen);
           flip(screen);
        }
     }
  
     cleanUp();
     SDL_KillThread(thread);
  
     return 0;
  }
  </code></pre>
  
  <p>The problem is that when I run it in Eclipse or in the terminal, I get this message:</p>
  
  <pre><code>XIO:  fatal IO error 0 (Success) on X server ":0.0"
    after 113 requests (113 known processed) with 0 events remaining.
  </code></pre>
  
  <p>or this one:</p>
  
  <pre><code>XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
    after 113 requests (113 known processed) with 2 events remaining.
  </code></pre>
  
  <p>I'm using Ubuntu 13 with Eclipse Kepler. I've tried to do some research, but wasn't able to find anything useful.</p>
  
  <p>Edit: So I updated the code to include a counter and a few cout's all over the place:</p>
  
  <pre><code>int myThread(void* data) {
     while (!quit) {
        //caption animation
        SDL_WM_SetCaption ("Thread is running", NULL);
        SDL_Delay(250);
  
        cout &lt;&lt; "Thread is running 1" &lt;&lt; endl;
  
        SDL_WM_SetCaption("Thread is running.", NULL);
        SDL_Delay(250);
  
        cout &lt;&lt; "Thread is running 2" &lt;&lt; endl;
  
        SDL_WM_SetCaption("Thread is running..", NULL);
        SDL_Delay(250);
  
        cout &lt;&lt; "Thread is running 3" &lt;&lt; endl;
  
        SDL_WM_SetCaption("Thread is running...", NULL);
        SDL_Delay(250);
  
        cout &lt;&lt; "Thread is running 4" &lt;&lt; endl;
     }
  
     return 0;
  }
  
  int main(int argc, char* argv[]) {
     SDL_Surface* screen = init(640, 480, "");
     XInitThreads();
  
     Surface test("resources/image.png");
     thread = SDL_CreateThread(myThread, NULL);
     SDL_Event event;
     int count = 1;
  
     while (!quit) {
        if(SDL_PollEvent(&amp;event)) {
           if (event.type == SDL_QUIT) {
              quit = true;
              break;
           }
  
           fillScreen(screen, Surface::WHITE);
           applySurface(0, 0, test, screen);
           flip(screen);
  
           cout &lt;&lt; "finished iteration " &lt;&lt; count++ &lt;&lt; endl;
        }
     }
  </code></pre>
  
  <p>and here's the output from the console after running it once:</p>
  
  <pre><code>finished iteration 1
  Thread is running 1
  XIO:  fatal IO error 0 (Success) on X server ":0.0"
        after 113 requests (113 known processed) with 0 events remaining.
  </code></pre>
  
  <p>then, after running it a second time:</p>
  
  <pre><code>finished iteration 1
  finished iteration 2
  Thread is running 1
  Thread is running 2
  XIO:  fatal IO error 0 (Success) on X server ":0.0"
        after 116 requests (116 known processed) with 0 events remaining.
  </code></pre>