Reputation: 20790
I have to deal with a design restriction(dependency issues) to not use scope guard for a mutex.
Sample code:
template<typename MutexType>
class Test
{
MutexType myMutex;
public:
void f()
{
myMutex.Lock();
//code with many returns here depending on conditions
myMutex.Unlock();
}
};
Is there other safe way of dealing with multiple returns without scope guards? How did this work before the concept was widely used? Is goto
a good option instead of return
considering exceptions are disabled?
Upvotes: 1
Views: 228
Reputation: 171167
As @VladLazarenko suggests in the comments, the best approach would be to simply write your own scope guard for this.
However, if for some obscure reason that's not possible, you can simply move all the //code with many returns here depending on conditions
into a separate function and just call that function between the Lock()
and Unlock()
calls.
You mention you have exceptions disabled; if that was not the case, the above solution (nested function) would require an additional try ... catch
construct in the outer function, then it would work as well.
Upvotes: 6