int getVal() {
this->_mutex.lock();
int result = this->_val;
this->_mutex.unlock();
return result;
}
? , this->_val result, , , , , . , , . .
void setVal(int val)
{
this->_mutex.lock();
this->_val = val;
this->_mutex.unlock();
}
Similarly, what are you buying? If you call setVal(-5)and setVal(17)of the separate streams at the same time, what value should be after completion? You encountered some problems to make sure that the first one to start will also be the first, but how to do this to get the “right” value?
source
share