Looks like I am having some multi-threading issues with my application but to be honest I am not sure what is going on.
Initialy, I though that the problem had something to do with the fact I load my model in a separate thread and then I add it to the scene from the main thread or using an update callback. But it turns out that the problem might appear even if I do not load my model in a separate thread.
The problem arises when I try to add extra views to my composite viewer. At the startup of the application I create a composite viewer, a main view and "N" extra views. I only add the main view to the composite viewer and everything runs fine. The other three views may have their contexts sharing the resources with main view context or may not. May share the same scene with main view or may not (viewer::setSceneData(NULL)). It doesn't matter, the problem appears in all cases.
Here is the problem now:
When I try to add the views to the composite viewer the application freezes. I tried to stop threading before adding a view and start threading again after adding them, but I still have the same problem. The threading mode composite viewer uses is "CullThreadPerCameraDrawThreadPerContext". The problem won't appear if: I stop the threading and not restart it again or use one of the following modes: DrawThreadPerContext, SingleThreaded.
Looks like I have some deadlock in my application. Using the debugger I can see that when my application freezes a lot of threads are waiting blocked:
The main thread blocks at OpenThreads::cooperateveWait() in Win32Thread.cpp line 55. If I move backwards on the stack, looks like the whole problem starts at the line 846 of ViewerBase.cpp:
_endDynamicDrawBlock->block(); If I check the member variables of the _endDynamicDrawBlock pointer I see that:
_blockCount = number of total views attached to the composite viewer
_currentCount = always 1
Using the debugger I can also see that other N + 1 threads are blocked (again, where N the number of views attached to the composite viewer):
The N threads (I guess these are the rendering theads, one for each viewer) are blocked at OpenThreads::cooperateveWait in Win32Thread.cpp line 50. If I walk the call stack backwards the problem starts at OperationThread::run() in OperationThread.cpp line 426: "(*operation)(_parent.get());"
The extra (+1) thread (no idea what this thread is for) is blocked at OpenThreads::cooperateveWait in Win32Thread.cpp line 50. If I walk the call stack backwards the problem starts at Renderer::draw() in Renderer.cpp line 649: "osgUtil::SceneView* sceneView = _drawQueue.takeFront();"
* Any idea what may be causing these deadlocks?
* What is the right way of adding/removing extra views to a composite viewer who is already running?
Btw: I am using OSG version 3.0.0
Thanks a lot for your time and sorry for the long post!
EDIT: The problem mostly appears when I try to add the views for second time (app_startup->addViews->removeViews->addViews(freezes here)). Although sometimes it might freeze from the first try or 3rd, 4th etc. Keep in mind that the main view is added once at the start and stays there until the termination of the application.