I once saw a snip of code as below,
/** Starts a synchronized block
*
* This macro starts a block synchronized on its argument x
* Note that the synchronized block defines a scope (i.e. { })
* All variables declared in it will live inside this block only
*/
#define SYNCHRONIZE_ON(x) { \
const abcd::LockBase & __lock = \
abcd::MakeLock(x); __lock;
/** Ends a synchronized block */
#define END_SYNCHRONIZE }
The SYNCHRONIZE_ON and END_SYNCHRONIZE are used together to synchronize on an object.
The macro SYNCHRONIZE_ON defines a variable ____lock in it’s block.
The question here is: what is the sentence __lock; (after the abcd::MakeLock(x);) for? Notice that this sentence consist of only the variable name.
Image you have this code:
It’ll be rewritten to:
Actually the
__lockvariable won’t be used in your code, it’s there only to dispose the critical section (if it’s what it uses) when it’ll go out of scope.The problem with that code is that it’ll generate tons of warning because
__lockis declared but never used. That statement is there to prevent those warnings and it’ll be optimized away by the compiler (because it has no side effects), this is what your compiler will actually perform:EDIT
This code should suppress “unused variable” warning but it may not save from “expression has no effect” warning.
pragmas to disable specific warning aren’t portable at all and Internet is full of workarounds to avoid them, like this (it seems to be pretty portable across compilers).In this case a better solution may be to split declaration and lock acquisition, like this: