Error while Multi-Threading

Started by thephpdev, October 21, 2012, 06:47:33 PM

Previous topic - Next topic

thephpdev

In my attempts to multi-thread my program, I have run into troubles with getting it to work with JPCT. Here is my error:
[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:

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?

EgonOlsen

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.

thephpdev

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?

EgonOlsen

#3
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.

thephpdev

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.

EgonOlsen

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?

thephpdev

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.

EgonOlsen

What kind of machine do you run this on?

thephpdev

Fedora Linux, Intel Pentium, 4GB Memory, 500GB Hard Drive, 2 cores, hyperthreaded.

EgonOlsen