Use spin_mutex inside a class

Use spin_mutex inside a class

imagem de Pourya Shirazian

I have a mesh class that is written into by "copyFrom" function and being read by "draw" function.

Creating the mesh is done by Parallel_For structure but drawing part is in main thread. Some how lock is not working and it grants access to a non-complete mesh be drawn. What is the problem with my mutex?

My code is:

typedef spin_mutex CMESH_SPINMUTEX;

class CMesh
{
private:
CMESH_SPINMUTEX m_mutex;

public:

//Writer

void copyFrom(const CMesh & other){

CMESH_SPINMUTEX::scoped_lock lock(m_mutex); copyVertices(other.m_lstVertices); copyTriangles(other.m_lstTriangles);

}

//Reader

void draw(CMaterial mtrl, bool bWireFrame = true, int objectID = 0){

TRIANGLE *t;

POLYVERTEX *pv0;

POLYVERTEX *pv1;

POLYVERTEX *pv2;

{

CMESH_SPINMUTEX::scoped_lock lock(m_mutex);

glPushAttrib(GL_ALL_ATTRIB_BITS);

glFrontFace(GL_CCW);

CMaterial::glSetMaterial(mtrl);

if(bWireFrame)

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

else

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glPushName(objectID);

glBegin(GL_TRIANGLES);

size_t ctTriangles = m_lstTriangles.size();

for(size_t i=0; i < ctTriangles; i++)

{

t = m_lstTriangles[i];

pv0 = m_lstVertices[t->v0];

glNormal3f(pv0->normal.x, pv0->normal.y, pv0->normal.z);

glVertex3f(pv0->vertex.x, pv0->vertex.y, pv0->vertex.z);

pv1 = m_lstVertices[t->v1];

glNormal3f(pv1->normal.x, pv1->normal.y, pv1->normal.z);

glVertex3f(pv1->vertex.x, pv1->vertex.y, pv1->vertex.z);

pv2 = m_lstVertices[t->v2];

glNormal3f(pv2->normal.x, pv2->normal.y, pv2->normal.z);

glVertex3f(pv2->vertex.x, pv2->vertex.y, pv2->vertex.z);

}

glEnd();

glPopName();

glPopAttrib();

}

}

};

Pourya Shirazian
1 post / novo 0
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.