Hi,
I have multiple OSG models to read into the application. When I read model by
model it is taking time to complete reading of all models. So I used multiple
threads to load files in less time.
Hre is the code snippet
Global Declarations
Code:
struct ModelObj
{
Model *obj;
int id;
};
HANDLE T[NUM_OF_MODELS];
UINT ThreadProc1(LPVOID lpvoid)
{
ModelObj *temp = (ModelObj*)lpvoid;
temp->obj->mt[temp->id] = new osg::MatrixTransform;
temp->obj->modelSwitch->addChild(temp->obj->mt[temp->id].get());
temp->obj->model[temp->id] =
osgDB::readNodeFile(temp->obj->fileNames[temp->id]);
if(temp->obj->model[temp->id])
{
temp->obj->mt[temp->id]->addChild(temp->obj->model[temp->id].get());
temp->obj->mt[temp->id]->addChild(temp->obj->sound_root.get());//OSGSoundLIB
temp->obj->mt[temp->id]->setUpdateCallback(
temp->obj->soundCB.get() );//OSGSoundLIB for sound
}
return 0;
}
In Main
Code:
ModelObj obj[NUM_OF_MODELS];
for(int i=0;i<NUM_OF_MODELS;i++)
{
obj[i].id=i;
obj[i].obj=&model;
}
.............
DWORD ThreadId[NUM_OF_MODELS];
for(int i=0;i<NUM_OF_MODELS;i++)
{
T[i]=new HANDLE;
T[i]= (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc1,
(LPVOID)&obj[i], 0, &ThreadId[i]));
if(T[i]==NULL)
{
std::cout<<"Failed to Create Therad_"<<i <<": "<< GetLastError()<<std::endl;
}
}
WaitForMultipleObjects(NUM_OF_MODELS, T, TRUE, INFINITE);
for(int t=0;t<NUM_OF_MODELS;t++)
{
if(CloseHandle(T[t]) != 0)
{
//std::cout<<"Tr's handle was closed successfully!\n";
}
else
std::cout<<"Failed to close Tr's handle \n";
}
With this code the time is reduced around 90%. But some times the application
is carshing.
Does OSG readNodeFile(osgDB) will support multithreding?
Or is there any mistake in my code?
Can you please help me what could be the mistake?
...
Thank you!
Cheers,
Koduri
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48972#48972
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org