LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: imagenesis on November 28, 2008, 16:03:10

Title: Notify/wait help (trying to have OpenGL thread talk to main/network code thread)
Post by: imagenesis on November 28, 2008, 16:03:10
I need to initiliaze some stuff in a seperate thread and once that's finished tell the main thread to continue, wether or not the init was successful. Here is what I came up with...


         public void init() {
OGLThread = new Thread() {
public void run() {
try {
Display.setParent(OGLP); //API methods, not really relevant
Display.create();
OGLRunning = true;
} catch (LWJGLException e) {
e.printStackTrace();
OGLFailed();
} finally {
OGLStartLock.notify();
}
}
};
OGLThread.start();
try {
OGLStartLock.wait();
} catch(InterruptedException e) {
OGLFailed();
}
OGLInitDone();
System.out.println("Main.init end");
}
        public void OGLInitDone() {
if(OGLRunning) {
                      //continue initiliazing stuff that doesn't belong in the OpenGL thread (network code, other/stuff)
}
}


This is throwing the following exception:

java.lang.IllegalMonitorStateException: current thread not owner
   at java.lang.Object.wait(Native Method)
   at java.lang.Object.wait(Object.java:474)
   at common.Main.init(Main.java:99)
   at common.Main.<init>(Main.java:80)
   at common.Launcher.init(Launcher.java:14)
   at sun.applet.AppletPanel.run(AppletPanel.java:380)
   at java.lang.Thread.run(Thread.java:595)
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException: current thread not owner
   at java.lang.Object.notify(Native Method)
   at common.Main$2.run(Main.java:93)

Obviously my grasp of notify/wait/synchronization is a little off. :(
Title: Re: Notify/wait help (trying to have OpenGL thread talk to main/network code thr
Post by: imagenesis on November 28, 2008, 22:33:19
Somewhat solved.

I have changed the code to this...


            OGLThread = new Thread() {
public void run() {
try {
Display.setParent(OGLP);
Display.create();
OGLRunning = true;
} catch (LWJGLException e) {
e.printStackTrace();
OGLFailed();
} finally {
TLC.validate();
System.out.println("finally");
/*synchronized (OGLStartLock) {
System.out.println("OGLThread init done");
OGLStartLock.notify();
}*/
}
}
};
System.out.println("OGLThread init pre");
OGLThread.start();
synchronized (OGLStartLock) {
try {
OGLStartLock.wait();
} catch(InterruptedException e) {
OGLFailed();
}
}


I am getting every initliazed (no exceptions thrown and break points are reached) but the LWJGL screen is not updating. IE, it never turns black. I'm not sure where that actually happens in LWJGL but I think it's in the update method if it isVisible() or isDirty()

Just posting to not waste time. Don't bother replying really uless you know exactly what the problem is. I'll figure this out, just gotta put some breakpoints in lwjgl.
Title: Re: Notify/wait help (trying to have OpenGL thread talk to main/network code thread)
Post by: Matzon on November 28, 2008, 23:35:29
please make sure that all OGL access is done in the same thread  - else you will get into trouble.
Title: Re: Notify/wait help (trying to have OpenGL thread talk to main/network code thr
Post by: imagenesis on November 30, 2008, 15:03:13
Hey could you take another look at the second piece of code. I can't figure out why LWJGL doesn't completely initiliaze. It doesnt throw any warning but the screen never turns black and drawing to the canvas doesnt doesnt display anything.
Title: Re: Notify/wait help (trying to have OpenGL thread talk to main/network code thr
Post by: imagenesis on December 04, 2008, 11:17:42
Resolved btw, sorry for not posting a little earlier.
Title: Re: Notify/wait help (trying to have OpenGL thread talk to main/network code thread)
Post by: Matzon on December 04, 2008, 12:05:23
consider posting the solution for others with similar problem...
Title: Re: Notify/wait help (trying to have OpenGL thread talk to main/network code thread)
Post by: Larssen on December 04, 2008, 14:36:20
Yeah please supply us with the solution :)

I managed to multithread a JavaFrame and LWJGL screen and run them in different threads, but couldnt manage to tell the GlApp to display itself inside the canvas. Yes, the canvas doesnt turn black, with no errors.