Login  Register

Trying to understand NEWT threading model

Posted by ariekenb on Nov 27, 2010; 2:18pm
URL: https://forum.jogamp.org/Trying-to-understand-NEWT-threading-model-tp1977447.html

I'm trying to understand the NEWT threading model using the RedSquare demo (https://github.com/sgothel/jogl-demos/blob/master/src/demos/es2/RedSquare.java)

If I run RedSquare with no command line arguments, it does the following:

1. Main creates one RedSquare and calls start on it.  This creates a thread named Thread-0.  Thread-0 runs RedSquare.run, which sits in a loop calling display() while the boolean quit is not true.  So RedSquare.display(GLAutoDrawable drawable) is continually being called from Thread-0.

2. RedSquare registers mouse and key listeners.  These listeners are invoked from the EDT created by NEWT for the display.  When I run this EDT is named Thread-0-Display-X11_:0.0-1-EDT.

Suppose the user hits the q key.  RedSquare.keyTyped will be invoked from the NEWT EDT (Thread-0-Display-X11_:0.0-1-EDT) and quit will be set to true.  Thread-0 in RedSquare.run will read quit and maybe if it is lucky see that it is true, but this is not guaranteed by the java memory model.  At the very least I think quit should be volatile.

So I am a bit confused by this example because it seems to be not very thread safe.  What is the recommended model for handling key/mouse events in NEWT and handling them in a thread safe way?  Setting non-synchronized variables and hoping for the best does not seem like a good idea. :)

I am used to the normal Swing/AWT model of all events and paint() actions being run from the AWT EDT.  The difference in this NEWT example is display() is being called from some other thread, while key and mouse events come in from the NEWT EDT.  Is there a way to have all rendering in NEWT happen in the NEWT EDT?  I suppose I could use com.jogamp.newt.Display.getEDTUtil().invoke() to do rendering - is this recommended?