ここに示すように、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 を使用しています。いくつかの調査を試みましたが、有用なものを見つけることができませんでした。
編集:そこで、コードを更新して、カウンターといくつかのカウトをいたるところに含めました:
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.