Boost::thread mutex issue: Try to lock, access violation

Posted by user1419305 on Stack Overflow See other posts from Stack Overflow or by user1419305
Published on 2012-12-13T22:51:57Z Indexed on 2012/12/13 23:03 UTC
Read the original article Hit count: 305

I am currently learning how to multithread with c++, and for that im using boost::thread. I'm using it for a simple gameengine, running three threads.

Two of the threads are reading and writing to the same variables, which are stored inside something i call PrimitiveObjects, basicly balls, plates, boxes etc.

But i cant really get it to work, i think the problem is that the two threads are trying to access the same memorylocation at the same time, i have tried to avoid this using mutex locks, but for now im having no luck, this works some times, but if i spam it, i end up with this exception:

First-chance exception at 0x00cbfef9 in TTTTT.exe: 0xC0000005: Access violation reading location 0xdddddded.
Unhandled exception at 0x77d315de in TTTTT.exe: 0xC0000005: Access violation reading location 0xdddddded.

These are the functions inside the object that im using for this, and the debugger is also blaming them for the exception.

void PrimitiveObj::setPos(glm::vec3 in){
 boost::mutex mDisposingMutex; 
 boost::try_mutex::scoped_try_lock lock(mDisposingMutex);
  if ( lock)
  {
    position = in;
     boost::try_mutex::scoped_try_lock unlock(mDisposingMutex);
  }
}


glm::vec3 PrimitiveObj::getPos(){
boost::mutex myMutex; 
 boost::try_mutex::scoped_try_lock lock(myMutex);
  if ( lock)
  {
   glm::vec3 curPos = position;
   boost::try_mutex::scoped_try_lock unlock(myMutex);
    return curPos;       
  }
  return glm::vec3(0,0,0);
}

Any ideas?

© Stack Overflow or respective owner

Related posts about c++

Related posts about access-violation