I've been using C# for the last few years and I am currently bug fixing in C++. In C# I could use lock on an object to make my code thread safe with:
lock(lockObject)
{
// Do some work
}
This would unlock the lockOject if there was an exception within the // Do some work
Is there something similar in C++? At the moment I can think of either:
// Code
{
AutoLock lock(lockObject);
// Do some work
}
// More Code
But I don't like the curly braces just to scope my AutoLock
. At the moment I'm doing:
AutoLock lock(lockObject);
// Do some work
lock.Unlock();
And letting exception unwinding release the lock if there's an exception in //Do some work
.
What I'm doing at the moment works, but I'm wondering if there's a better way, thanks.