I am attempting to create an application which uses Boost::Thread in order to complete a task. I need the program to load data in the background while displaying the data which has currently been loading.
Currently I have a list of Objects which all have Update and Render methods. In the update function if something needs to load I am attempting to create a thread for the loading. This way I can continuously render without delays from loading. The render only needs to be blocked from occurring when the data is finished loading and being setup for use (binding a texture in OpenGL)
If I want to create a thread of the Update function in Object I have to declare it as a static method correct? Since static methods are unable to access the non-static members of the Object how do I block the Render method when I need.
I thought this was where I would use a scoped_lock mutex. Everything I have attempted so far does not seem to work. Making the mutex a member of the Object does not work as the static method does not have access. If I make the mutex static outside of the class I'm confused as to how that will work with multiple update threads running at once. I'd assume things would become very confusing in that situation.
Thanks for all your help.
Code://Code taken from Object::Update()
if(discardLevel != prevDiscardLevel)
{
prevDiscardLevel = discardLevel;
//LoadTexture is a static method of Object
boost::thread updateThread(boost::bind(&LoadTexture, texture, glTex, discardLevel));
}
//Code taken from Object::updateThread()
void Object::LoadTexture(Texture* Tex, GLuint &texData, int dLevel)
{
Tex->LoadTexture(dLevel); //Load specific textures data
boost::mutex::scoped_lock lock(tex2GL);
glBindTexture(GL_TEXTURE_2D, texData); //Bind the texture for use
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Tex->GetWidth(),
Tex->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, Tex->GetTexture());
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
//Code taken from Object::Render()
void Object::Render()
{
if(!visible)
return;
boost::mutex::scoped_lock lock(tex2GL);
glPushMatrix();
glTranslatef(transform.x3y0, transform.x3y1, transform.x3y2);
glBindTexture(GL_TEXTURE_2D, glTex);
glBegin(GL_QUADS);
glTexCoord2f(1.f, 0.f);
glVertex3f(-texture->GetHalfWidth(),-texture->GetHalfHeight(), 0.0f);
glTexCoord2f(0.f, 0.f);
glVertex3f(texture->GetHalfWidth(),-texture->GetHalfHeight(), 0.0f);
glTexCoord2f(0.f, 1.f);
glVertex3f(texture->GetHalfWidth(),texture->GetHalfHeight(), 0.0f);
glTexCoord2f(1.f, 1.f);
glVertex3f(-texture->GetHalfWidth(),texture->GetHalfHeight(), 0.0f);
glEnd();
glPopMatrix();
}