我正在尝试使用 C++ 和 SDL 创建一个简单的多线程示例,如下所示:
#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.
我正在使用带有 Eclipse Kepler 的 Ubuntu 13。我试图做一些研究,但找不到任何有用的东西。
编辑:所以我更新了代码以包括一个计数器和一些 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.