Run code while locking a mutex.
Declared in <sync.h>
#define WITH_LOCK(cs, code)
Examples:
WITH_LOCK(cs, shared_val = shared_val + 1);
int val = WITH_LOCK(cs, return shared_val);
Note:
Since the return type deduction follows that of decltype(auto), while the deduced type of:
WITH_LOCK(cs, int i = 1; return i);
is int, the deduced type of:
WITH_LOCK(cs, int j = 1; return (j));
is &int, a reference to a local variable
The above is detectable at compile-time with the -Wreturn-local-addr flag in gcc and the -Wreturn-stack-address flag in clang, both enabled by default.
| Name | Description |
|---|---|
| cs | Mutex to lock while the code runs. |
| code | Statements to execute while holding the lock; its value is returned. |