[#WITH_LOCK]
= WITH&lowbar;LOCK
:mrdocs:

Run code while locking a mutex&period;

== Synopsis

Declared in `&lt;sync&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
#define WITH&lowbar;LOCK(cs, code)

----

== Description

Examples&colon;

WITH&lowbar;LOCK(cs, shared&lowbar;val &equals; shared&lowbar;val &plus; 1)&semi;

int val &equals; WITH&lowbar;LOCK(cs, return shared&lowbar;val)&semi;

Note&colon;

Since the return type deduction follows that of decltype(auto), while the deduced type of&colon;

WITH&lowbar;LOCK(cs, int i &equals; 1&semi; return i)&semi;

is int, the deduced type of&colon;

WITH&lowbar;LOCK(cs, int j &equals; 1&semi; return (j))&semi;

is &amp;int, a reference to a local variable

The above is detectable at compile&hyphen;time with the &hyphen;Wreturn&hyphen;local&hyphen;addr flag in gcc and the &hyphen;Wreturn&hyphen;stack&hyphen;address flag in clang, both enabled by default&period;

== Parameters

[cols="1,4"]
|===
| Name| Description
| *cs*
| Mutex to lock while the code runs&period;
| *code*
| Statements to execute while holding the lock&semi; its value is returned&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
