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?