<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">GUARDED_BY: It is true that the C++ compiler does not know which mutex<br>
guards what variables, and therefore cannot alert you if you, say, use<br>
a variable without locking its mutex. I solved this problem in a<br>
commercial project by defining two templates: a guard and an accessor.<br>
The guard contained the mutex and the variable (or struct of<br>
variables) as a private member, and only the accessor could access it.<br>
The accessor would lock and unlock the mutex in a RAII manner. An<br>
example using this technique could look like this:<br>
<br>
class Alpha<br>
{<br>
public:<br>
int GetValue() const<br>
{<br>
const_unique_access<int> value(valueGuard);<br>
return *value;<br>
}<br>
void SetValue(int v)<br>
{<br>
unique_access<int> value(valueGuard);<br>
*value = v;<br>
}<br>
private:<br>
unique_access_guard<int> valueGuard;<br>
};<br></blockquote><div><br></div><div>Interesting idea - it has some parallels with a lambda-based more functional-programming inspired device similar to boost::optional I've been toying with. Using that type of technique you could do something like this:</div>
<div><br></div><div>uniquely_accessed<int> value;<br>...<br>value.Access([](const int& i)<br>{<br> ...<br>});<br>...<br>value.Mutate([](int& i)<br>{<br> ...<br>});<br> <br>Just as another take on the same construct.</div>
</div>