[osg-users] osg::Operation lags

Paul Melis paul at science.uva.nl
Fri Mar 20 06:23:49 PDT 2009


Hi Robert,

Robert Osfield wrote:
> I have never seen problems like yours when using 
> OperationThread/Operatons, but on a second review I see that your 
> OpenThread never sleeps, it just churns away as effectively a spin 
> lock on the CPU. If your machine doesn't have sufficient cores to 
> handle a thead using 100% of the CPU core then you'll get frame stalls 
> like you have.
More general, is what follows a good summary of how to use an 
osg::Operation (looking at the osgtext example for reference)? You 
subclass it and implement your action in the call operator. Then you add 
an instance of the operation class to a separate thread (starting it so 
it calls the operation in the background), plus you add the same 
instance to a osgViewer::Viewer instance. The only argument to the call 
operator is used to distinguish who makes a call on the operation and 
therefore what action you need to take. Is that a good summary?

Ignoring having to call yield once in a while, is the below test roughly 
the way to do it (I used the osgtext example as a second source)? Is the 
mutex in the call operator necessary?

Doh, lots of question marks above :)
Paul

// g++ -g -o op operation.cpp -I ~/osg2.8/include/ -L ~/osg2.8/lib/ 
-losg -losgDB -losgViewer
#include <cstdio>
#include <osg/OperationThread>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>

class UpdateOperation : public osg::Operation
{
public:
    UpdateOperation():
        Operation("UpdateOperation", true)
    {
    }

    virtual void operator() (osg::Object* callingObject)
    {
        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
        printf("operator(): obj = 0x%08x\n", callingObject);
    }

    virtual void release()
    {
        printf("release()\n");
    }

protected:
    OpenThreads::Mutex          _mutex;
};

int
main()
{
    osg::ref_ptr<UpdateOperation> updateOperation = new UpdateOperation();

    osgViewer::Viewer viewer;

    osg::ref_ptr<osg::OperationThread> operationThread = new 
osg::OperationThread;
    operationThread->add(updateOperation.get());
    operationThread->startThread();

    viewer.addUpdateOperation(updateOperation.get());

    viewer.setSceneData(osgDB::readNodeFile("cow.osg"));

    viewer.run();
}






More information about the osg-users mailing list