A while age a colleague told me he spent lot of time debugging a race condition. The culprit turned out to be something like this:
void foo()
{
ScopedLock(this->mutex); // Oops, should have been a named object.
// Edit: added the "this->" to fix compilation issue.
// ....
}
In order to prevent situation from happening again he created the following macro after the definition of the ScopedLock class:
#define ScopedLock(...) Error_You_should_create_a_named_object;
This patch works fine.
Does anyone know any other interesting techniques to prevent this problem?
ScopedLock
in another namespace. The macro will still work namespace-qualified (since it produces an error anyway). – FrothyScopedLock
in another namespace. Or specifically, to write code so that you don't care whether or not someone else likes the look of the nameScopedLock
for their class (function, or global variable). – BarriebarrientosScopedLock
in another namespace. – Frothy