我正在尝试使用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.
答案 0 :(得分:0)
如有疑问,请在主线程上执行所有SDL操作。
主要例外是SDL_PostEvent()
。
答案 1 :(得分:0)
啊没关系。我收到了一封电子邮件的回复,我发送给制作我正在关注的教程的人:
是的,我正在修复SDL 2.0教程的应用程序中的错误。 查看SDL 2.0多线程示例以获取修复。问题是SDL有 多线程做渲染不起作用。所以从另一个线程设置标题 不行。在2.0教程中,渲染是在一个线程和控制台输出中 在另一个。