thread question

Hello,

I’m investigating using Xith for adding 3D visualization support to a simulation framework (repast.sf.net). The simulations are not games exactly but they do have similarities (e.g move your simulated objects, then update the display, repeat until pause or stop).

The simulation executes in its own thread and I’m wondering what sort of issues this raises. Ideally, we’d want to iterate a step of the simulation in this thread and then tell the scene graph to update itself. Is this possible? Is my question entirely wrong headed?

thanks,

Nick

it’s possible … this way you avoid making simulation changes while the view of the scenegraph is rendering

Just make sure your modifications to the scenegraph and rendering of the scenegraph don’t happen at the same time, and you should be fine.

Java3D is thread-safe and allows modification of the scenegraph while it is rendering (and takes a performance hit for having this feature), perhaps it would be more suitable?

Will.

Thanks.

How do I do this? I’ve seen the docs and created some simple tests. The tests are similar to the first interaction demo (spin cube with arrow keys) but the cube just rotates on its own. The incrementing of rotX and rotY takes place in an update method. This method is repeatedly called in a while loop. However, the actual loop itself runs as part of another thread. I’ve synchronized the call to the actual update (view.renderOnce()) in the update method, but I occassionaly get errors indicating that the canvas is locked. So, I must be doing something incorrectly.

thanks,

Nick

[quote]Thanks.
How do I do this? I’ve seen the docs and created some simple tests. The tests are similar to the first interaction demo (spin cube with arrow keys) but the cube just rotates on its own. The incrementing of rotX and rotY takes place in an update method. This method is repeatedly called in a while loop. However, the actual loop itself runs as part of another thread. I’ve synchronized the call to the actual update (view.renderOnce()) in the update method, but I occassionaly get errors indicating that the canvas is locked. So, I must be doing something incorrectly.

thanks,

Nick
[/quote]
Yeah… Just have a thread that’s constantly calling view.renderOnce() (think while(true)). Actually, there’s several examples of how to do this on Xith’s Getting Started page. Just look at the example source. It basically involves checking if certain variables are set each time in the loop. How you actually implement this is up to you, but look at the source for some examples.

Nick,

Using threads and sharing resources between them is not something you should undertake lightly. There are many pit falls and gotchas just waiting to waste your time. I strongly advise that you learn about writing muti-threaded applications before doing one.

If you are simply processing input or network in a seperate thread, a common method is just to buffer it and processes it between renders.

Will.

Hi,

[quote]The simulation executes in its own thread and I’m wondering what sort of issues this raises. Ideally, we’d want to iterate a step of the simulation in this thread and then tell the scene graph to update itself. Is this possible?
[/quote]
Could be that the easiest way is to use your simulation thread as a rendering thread. You should take care of:

  1. Not calling View.startView(), because of it starts additional { while(true) renderOnce() } rendering thread;

  2. Ensure that you do not attempt to call renderOnce() from other threads - OpenGL contexts are thread-oriented.

Yuri

Thanks! That’s what I was looking for. I’ve written several multi-threaded apps, but wasn’t sure where the rendering thread got started etc. in Xiph. I should have looked at the source, of course.

thanks again,

Nick