我試圖創建使用C++和SDL的簡單的多線程例子如下所示:SDL和X11多線程致命IO錯誤
#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;
}
的問題是,當我在Eclipse或在終端運行它,我得到這個消息:
XIO: fatal IO error 0 (Success) on X server ":0.0"
after 113 requests (113 known processed) with 0 events remaining.
或者這一個:
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
after 113 requests (113 known processed) with 2 events remaining.
我使用Ubuntu 13與Eclipse開普勒。我試圖做一些研究,但無法找到有用的東西。
編輯:所以我更新的代碼,包括櫃檯和幾個COUT的所有的地方:
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;
}
}
,並在這裏一次運行之後是從控制檯輸出:
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.
然後在第二次運行之後:
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.
程序在存在之前是否打印*任何*輸出? – SevenBits
是的,我在while循環中添加了一個計數器,並在主線程和SDL創建的線程中添加了一些cout。它在幾毫秒內打印出成功的活動,然後終止。 – solstice333