[osg-users] can I use multi thread for readNodeFile?

Lilinx lilinx at 21cn.com
Mon Sep 24 03:41:38 PDT 2007


hi,all
I want to use multithread to read many small file , I test the following code, but it seen as if some bugs in it.
help me! thanks!
 
class ReadNodeFileThread:public OpenThreads::Thread
{
public:
	ReadNodeFileThread(void){
		_fileName = "";
		_loadOptions = 0;
		_hadLoaded   = false;
		_done        = false;
	}

	virtual void run()
	{
		while(true)
		{
			_mutex.lock();

			if(_done) break;

			if(!_hadLoaded)
			{
				_loadedModel = osgDB::readNodeFile(_fileName,_loadOptions.get());
				_hadLoaded = true;
			}
			_mutex.unlock();

			OpenThreads::Thread::YieldCurrentThread();
			
		}
	}

	void setParam(const std::string& filename,const osgDB::ReaderWriter::Options* options )
	{
		_mutex.lock();
		_fileName = filename;
		_loadOptions = options?static_cast<osgDB::ReaderWriter::Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)):new osgDB::ReaderWriter::Options;
		_hadLoaded   = false;
		_mutex.unlock();
	}

	osg::Node* getLoadedModel()
	{
		osg::Node* node=0;
		_mutex.lock();
		if(_hadLoaded) 
			node =	_loadedModel.get();
		std::cout<<"loadfinished"<<_fileName<<std::endl;
		_mutex.unlock();


		return node;
	}

	void setDone(bool done)
	{
		_mutex.lock();
		_done = true;
		_mutex.unlock();
	}

protected:
	~ReadNodeFileThread(void){
	}

	osg::ref_ptr<osg::Node>						_loadedModel;
	std::string									_fileName;
	osg::ref_ptr<osgDB::ReaderWriter::Options>	_loadOptions;
	bool										_hadLoaded;

	bool										_done;

	OpenThreads::Mutex							_mutex;
};

				 
                                                                  Lilinx  2007 09 24     
       


More information about the osg-users mailing list