Re: [osg-users] Running viewer loop and physic calculations in different threads
Cory, Paul and all, Thank you for your answers. I have finally solved it by using Intel's TBB, among which there are some functions to launch threads. Best regards, Alfonso 2009/5/22 Alfonso Callejo Goena > Hello everyone: > > I'm developing a OSG-based graphic engine for a real time mechanical > simulator. I would like that the physics and the graphic display would run > in separate threads so that they could work independently without bothering > each other. The communication between them is a kind of buffer or list to > which the physics program pushes the last calculation and the graphic > program pulls the appropriate one. > I don't know which is the best (and most simple) way of setting up and > managing the threads. Does the osgViewer class have a way of doing this? > Where can I find an example which follows this technique? > > Thank you, > Alfonso > ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Running viewer loop and physic calculations in different threads
Alfonso, I've distilled my code down. I hope I haven't cut out too much. The most complicated part for me was getting the halting sequence correct. I tell the viewer to stop drawing and then have to wait until the viewer thread is finished and notifies me that it is complete. I have a view class that "owns" the scene graph and the display. It looks something like: class MyView { private: /// the OSG view onto the scene osg::ref_ptr m_viewer; /// the thread sets this to {...@code true} when the viewer thread has stopped bool m_viewerThreadStopped; /// the mutex to protect access to {...@link #m_viewerThreadStopped} boost::mutex m_viewerThreadStoppedMutex; /// the condition variable for signalling {...@link #m_viewerThreadStopped} has been set boost::condition_variable m_viewerThreadStoppedCond; public: MyView() : m_viewerThreadStopped(false) { } ~MyView(); void initialize(); ... }; MyView::~MyView() { if (m_viewer) { boost::unique_lock lock(m_viewerThreadStoppedMutex); m_viewer->setDone(true); // this while loop has to be here because of spurious wakes while (!m_viewerThreadStopped) { // unblock and wait for it to signal that it is done running m_viewerThreadStoppedCond.wait(lock); } m_viewer->stopThreading(); } } void MyView::initialize() { // set up the scene graph here ... // start the thread that does the OSG rendering boost::thread(startThread, m_viewer, &m_viewerThreadStopped, &m_viewerThreadStoppedMutex, &m_viewerThreadStoppedCond); } void startThread(osg::ref_ptr pViewer, bool* doneFlag, boost::mutex* mutex, boost::condition_variable* cond) { pViewer->run(); boost::lock_guard lock(*mutex); *doneFlag = true; cond->notify_all(); } Alfonso Callejo Goena wrote: Cory Riddell writes: Hi Alfonso, I'm doing something similar to what you are doing. My viewer is running in it's own thread. I have a shared object that I use to communicate between the threads. If you are looking for sample code, I can try to distill what I have to something relatively simple. I'm using the boost threads library. It seems to work quite well and is cross platform. Have you written multi-threaded apps before? Are you familiar with mutexes (mutices?), conditions, volatile variables, spurious wakes, etc...? If you aren't familiar with the boost stuff, I would recommend spending some time getting familiar with it. Aside: if anybody can explain why spurious wakes exist, I'd love to hear an explanation. Cory Hi Cory, I would appreciate very much your sample code, because I don't have much experience in this issue. Did you consider using what Paul has suggested (osgDB::DatabasePager)? In my case the physical calculations are in fact a standalone program, and it is not just "loaded data"... Thank you very much, Alfonso ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Running viewer loop and physic calculations in different threads
Cory Riddell writes: > > > Hi Alfonso, > I'm doing something similar to what you are doing. My viewer is running > in it's own thread. I have a shared object that I use to communicate > between the threads. If you are looking for sample code, I can try to > distill what I have to something relatively simple. > I'm using the boost threads library. It seems to work quite well and is > cross platform. Have you written multi-threaded apps before? Are you > familiar with mutexes (mutices?), conditions, volatile variables, > spurious wakes, etc...? > If you aren't familiar with the boost stuff, I would recommend spending > some time getting familiar with it. > Aside: if anybody can explain why spurious wakes exist, I'd love to > hear an explanation. > Cory Hi Cory, I would appreciate very much your sample code, because I don't have much experience in this issue. Did you consider using what Paul has suggested (osgDB::DatabasePager)? In my case the physical calculations are in fact a standalone program, and it is not just "loaded data"... Thank you very much, Alfonso ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Running viewer loop and physic calculations in different threads
Hi Alfonso, I'm doing something similar to what you are doing. My viewer is running in it's own thread. I have a shared object that I use to communicate between the threads. If you are looking for sample code, I can try to distill what I have to something relatively simple. I'm using the boost threads library. It seems to work quite well and is cross platform. Have you written multi-threaded apps before? Are you familiar with mutexes (mutices?), conditions, volatile variables, spurious wakes, etc...? If you aren't familiar with the boost stuff, I would recommend spending some time getting familiar with it. Aside: if anybody can explain why spurious wakes exist, I'd love to hear an explanation. Cory Alfonso Callejo Goena wrote: Hello everyone: I'm developing a OSG-based graphic engine for a real time mechanical simulator. I would like that the physics and the graphic display would run in separate threads so that they could work independently without bothering each other. The communication between them is a kind of buffer or list to which the physics program pushes the last calculation and the graphic program pulls the appropriate one. I don't know which is the best (and most simple) way of setting up and managing the threads. Does the osgViewer class have a way of doing this? Where can I find an example which follows this technique? Thank you, Alfonso ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
[osg-users] Running viewer loop and physic calculations in different threads
Hello everyone: I'm developing a OSG-based graphic engine for a real time mechanical simulator. I would like that the physics and the graphic display would run in separate threads so that they could work independently without bothering each other. The communication between them is a kind of buffer or list to which the physics program pushes the last calculation and the graphic program pulls the appropriate one. I don't know which is the best (and most simple) way of setting up and managing the threads. Does the osgViewer class have a way of doing this? Where can I find an example which follows this technique? Thank you, Alfonso ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org