简体   繁体   中英

SDL and X11 multithreading Fatal IO Error

I'm trying to create a simple multithreading example using C++ and SDL as shown here:

#include <iostream>
#include "SDL/SDL_image.h"
#include "SDL/SDL.h"
#include "SDL/SDL_thread.h"
#include "X11/Xlib.h"
#include "SDLAbstractionLayer.h"
#include <string>

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(&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;
}

The problem is that when I run it in Eclipse or in the terminal, I get this message:

XIO:  fatal IO error 0 (Success) on X server ":0.0"
  after 113 requests (113 known processed) with 0 events remaining.

or this one:

XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
  after 113 requests (113 known processed) with 2 events remaining.

I'm using Ubuntu 13 with Eclipse Kepler. I've tried to do some research, but wasn't able to find anything useful.

Edit: So I updated the code to include a counter and a few cout's all over the place:

int myThread(void* data) {
   while (!quit) {
      //caption animation
      SDL_WM_SetCaption ("Thread is running", NULL);
      SDL_Delay(250);

      cout << "Thread is running 1" << endl;

      SDL_WM_SetCaption("Thread is running.", NULL);
      SDL_Delay(250);

      cout << "Thread is running 2" << endl;

      SDL_WM_SetCaption("Thread is running..", NULL);
      SDL_Delay(250);

      cout << "Thread is running 3" << endl;

      SDL_WM_SetCaption("Thread is running...", NULL);
      SDL_Delay(250);

      cout << "Thread is running 4" << 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(&event)) {
         if (event.type == SDL_QUIT) {
            quit = true;
            break;
         }

         fillScreen(screen, Surface::WHITE);
         applySurface(0, 0, test, screen);
         flip(screen);

         cout << "finished iteration " << count++ << endl;
      }
   }

and here's the output from the console after running it once:

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.

then, after running it a second time:

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.

When in doubt do all SDL operations on the main thread.

The primary exception is SDL_PostEvent() .

Ah never mind. I just got a reply from an email I sent to the person who made the tutorial I'm following:

Yeah that's bug in my application which I am fixing the in the SDL 2.0 tutorial. Check out the SDL 2.0 multithreading example for a fix. The problem is that SDL having multiple threads do rendering doesn't work. So setting the caption from a another thread doesn't work right. In the 2.0 tutorial, rendering is in one thread and console output is in another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM