Are the two code samples below equivalent?
Poco::ProcessHandle::PID ProcessRunner::processId() const
{
Poco::ProcessHandle::PID pid = 0;
mMutex.lock();
pid = mPID;
mMutex.unlock();
return pid;
}
,
Poco::ProcessHandle::PID ProcessRunner::processId() const
{
Poco::ScopedLock<Poco::Mutex> lock(mMutex);
return mPID;
}
- In the second sample: will the lock go out of scope after the return value copy has been made? This would matter if an object was returned that had many instructions for copying.
- Is a lock necessary if you are only going to return an int value? Or is the copying of the int an atomic operation?