0

我正在尝试使用 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.
4

2 回答 2

0

如有疑问,请在主线程上执行所有 SDL 操作。

主要的例外是SDL_PostEvent().

于 2013-08-28T20:59:38.847 回答
0

啊没关系。我刚刚收到一封电子邮件的回复,我发给了我正在关注的教程的制作人:

是的,这是我在 SDL 2.0 教程中修复的应用程序中的错误。查看 SDL 2.0 多线程示例以获得修复。问题是具有多个线程进行渲染的 SDL 不起作用。因此,从另一个线程设置标题不起作用。在 2.0 教程中,渲染在一个线程中,控制台输出在另一个线程中。

于 2013-08-29T07:33:24.230 回答