Added support for running multiple text generation threads

This commit is contained in:
Robert Osfield 2007-08-31 16:59:32 +00:00
parent 4ba3f3c1a1
commit 8d1ef9906a

View File

@ -604,40 +604,49 @@ int main(int argc, char** argv)
// construct the viewer. // construct the viewer.
osgViewer::Viewer viewer(arguments); osgViewer::Viewer viewer(arguments);
osg::ref_ptr<osg::OperationThread> operationThread; typedef std::list< osg::ref_ptr<osg::OperationThread> > Threads;
Threads operationThreads;
osg::ref_ptr<UpdateTextOperation> updateOperation; osg::ref_ptr<UpdateTextOperation> updateOperation;
if (arguments.read("--mt")) unsigned int numThreads = 0;
if (arguments.read("--mt", numThreads) || arguments.read("--mt"))
{ {
// construct a multi-threaded text updating test. // construct a multi-threaded text updating test.
if (numThreads==0) numThreads = 1;
// create a group to add everything into. // create a group to add everything into.
osg::Group* mainGroup = new osg::Group; osg::Group* mainGroup = new osg::Group;
osg::Group* textGroup = new osg::Group; for(unsigned int i=0; i<numThreads; ++i)
mainGroup->addChild(textGroup); {
osg::Group* textGroup = new osg::Group;
mainGroup->addChild(textGroup);
// create the background thread // create the background thread
operationThread = new osg::OperationThread; osg::OperationThread* operationThread = new osg::OperationThread;
// create the operation that will run in the background and operationThreads.push_back(operationThread);
// sync once per frame with the main viewer loop.
updateOperation = new UpdateTextOperation(textGroup);
// add the operation to the operation thread and start it. // create the operation that will run in the background and
operationThread->add(updateOperation.get()); // sync once per frame with the main viewer loop.
operationThread->startThread(); updateOperation = new UpdateTextOperation(textGroup);
// add the operation to the viewer to sync once per frame. // add the operation to the operation thread and start it.
viewer.addUpdateOperation(updateOperation.get()); operationThread->add(updateOperation.get());
operationThread->startThread();
// add the operation to the viewer to sync once per frame.
viewer.addUpdateOperation(updateOperation.get());
// add a unit cube for the text to appear within. // add a unit cube for the text to appear within.
osg::Geode* geode = new osg::Geode; osg::Geode* geode = new osg::Geode;
geode->getOrCreateStateSet()->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE)); geode->getOrCreateStateSet()->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE));
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.5f,0.5f,0.5f),1.0))); geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.5f,0.5f,0.5f),1.0)));
mainGroup->addChild(geode); mainGroup->addChild(geode);
}
viewer.setSceneData(mainGroup); viewer.setSceneData(mainGroup);
} }
@ -677,9 +686,14 @@ int main(int argc, char** argv)
viewer.run(); viewer.run();
if (operationThread.valid()) if (!operationThreads.empty())
{ {
operationThread->cancel(); for(Threads::iterator itr = operationThreads.begin();
itr != operationThreads.begin();
++itr)
{
(*itr)->cancel();
}
} }
} }