I'm working on a multi threaded demo app to test some program profiling tools for my company.
I spawn two threads and have a mutex to control access to a stack of objects.
I believe the top()
function is returning the reference and not a copy of my object to my threads... subsequently when I release the mutex after accessing the stack the other thread grabs the mutex. Something odd is happening.
A
std::stack<polygon> * polygonList = new std::stack<polygon>();
threadedFuntion()
{
polygon p = polygonList->top();
polygonList->pop();
ReleaseMutex(polyListMutex);
// preform point in polygon check
bool done = false;
while(!done)
{
done = p.doWork();
}
}
The program works when its structured like this
B
std::stack<polygon> * polygonList = new std::stack<polygon>();
threadedFuntion()
{
// preform point in polygon check
bool done = false;
while(!done)
{
done = polygonList->top().doWork();
}
polygonList->pop();
ReleaseMutex(polyListMutex);
}
But then its not releasing the mutex until its done doing its work.
I want the threads to grab a copy of the polygon object, take it off the stack, and release the mutex, then continue on doing its work.
EDIT I'm following this guide for mutexs and threading
EDIT Overriding operator=
polygon& polygon::operator=(const polygon &obj)
{
if (this == &obj)
return *this;
std::cout << "equals";
pa = obj.pa;
return *this;
}