Author Topic: Error while Multi-Threading  (Read 1388 times)

Offline thephpdev

  • byte
  • *
  • Posts: 5
    • View Profile
Error while Multi-Threading
« on: October 21, 2012, 06:47:33 pm »
In my attempts to multi-thread my program, I have run into troubles with getting it to work with JPCT. Here is my error:
Code: [Select]
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
java: xcb_io.c:178: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.

I have JPCT update the screen and render with a timer thread, which is shown here:
Code: [Select]
frameTimer = new Timer(1000 / FPS, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (!isInitialized) {
open();
update3D(chunk);
playerProcess.setWidthHeight(buffer.getOutputWidth(), buffer.getOutputHeight());
playerProcess.start();
viewChanged = true;
}
buffer.clear(Color.BLACK);
font.blitString(buffer, "Frame Rate:  " + currentFPS, 5, 15, 0, Color.WHITE);
font.blitString(buffer, "Target Rate: " + FPS, 5, 35, 0, Color.WHITE);
synchronized (world) {
synchronized (player) {
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
}
}
buffer.displayGLOnly();
viewChanged = false;
frameID++;
if (new Date().getTime() - frameSecondTime >= 1000) {
playerProcess.setSpeed(1.2f * (25f / FPS));
frameTimer.setDelay(1000 / FPS);
currentFPS = frameID;
frameID = 0;
frameSecondTime = new Date().getTime();
}
if (!running) {
close();
frameTimer.stop();
Thread.currentThread().interrupt();
}
}
});

And then I have another thread that modifies the camera position and rotations plus the player object, which acts as an easily modifiable class that I want to be able to change at a later date. I create a synchronized block every time I want to modify world or player, both in the timer thread and in my own processing thread. Any idea of how to fix this, or what's wrong?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Error while Multi-Threading
« Reply #1 on: October 21, 2012, 08:08:06 pm »
You can't access the current GL context from outside the thread that created it. I.e. you have to create the framebuffer from inside the thread that does the rendering.

Offline thephpdev

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Error while Multi-Threading
« Reply #2 on: October 21, 2012, 08:36:23 pm »
I create the framebuffer and do my rendering inside the timer thread, but I modify the world in the other thread - not the buffer. Is that an issue?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Error while Multi-Threading
« Reply #3 on: October 21, 2012, 10:33:13 pm »
Yes, it is but that doesn't explain the error message. That's not a message from jPCT but from your x-server or some related component. Why do you want to use multiple threads in the first place? It complicates things (as you can see...), so i would try to avoid it if possible or consider to use jPCT's own multi-threading support instead.
« Last Edit: October 21, 2012, 10:38:10 pm by EgonOlsen »

Offline thephpdev

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Error while Multi-Threading
« Reply #4 on: October 21, 2012, 10:53:59 pm »
I want to use multiple threads because I get far too low FPS currently, and since I plan on having this be a rather big game with many things going on I am going to multi-thread it. Why isn't the library synchronized anyway? Naturally many people would want to do other things, and have other threads move the character etc.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Error while Multi-Threading
« Reply #5 on: October 21, 2012, 11:25:37 pm »
If you synchronize everything by default, you end up with something like Java 3D...a jerky, unpredictable mess suitable for maybe something but not for games. If you want to use multiple threads, you have to do the synchronization by yourself. Anyway, if your fps are low, you might consider other options before aiming for multiple threads.

For example:



What does your game do? How is the scene composed (object count, polygon count,...)? And finally: What exactly is slow?  Rendering? Collisions? Something else?

Offline thephpdev

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Error while Multi-Threading
« Reply #6 on: October 22, 2012, 01:36:02 am »
The FPS is at around 5 naturally with collision and everything, and a polygon (triangle) count of around 12,288. When I remove collision detection I get something around 13-14, but I need collision detection. I have never used any of the items you list there, but it sounds like something I would like to do. I will have to figure out some alternatives to the threading.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Error while Multi-Threading
« Reply #7 on: October 22, 2012, 07:07:04 am »
What kind of machine do you run this on?

Offline thephpdev

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Error while Multi-Threading
« Reply #8 on: October 23, 2012, 05:13:19 am »
Fedora Linux, Intel Pentium, 4GB Memory, 500GB Hard Drive, 2 cores, hyperthreaded.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Error while Multi-Threading
« Reply #9 on: October 23, 2012, 07:09:26 am »
And the graphics card is?