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?