Notify/wait help (trying to have OpenGL thread talk to main/network code thread)

Started by imagenesis, November 28, 2008, 16:03:10

Previous topic - Next topic

imagenesis

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. :(

imagenesis

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.

Matzon

please make sure that all OGL access is done in the same thread  - else you will get into trouble.

imagenesis

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.

imagenesis


Matzon


Larssen

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.