[cfe-dev] Proposal for thread safety attributes for Clang

David Blaikie dblaikie at gmail.com
Sat Jul 2 17:40:56 PDT 2011


>
> GUARDED_BY: It is true that the C++ compiler does not know which mutex
> guards what variables, and therefore cannot alert you if you, say, use
> a variable without locking its mutex. I solved this problem in a
> commercial project by defining two templates: a guard and an accessor.
> The guard contained the mutex and the variable (or struct of
> variables) as a private member, and only the accessor could access it.
> The accessor would lock and unlock the mutex in a RAII manner. An
> example using this technique could look like this:
>
>   class Alpha
>   {
>   public:
>     int GetValue() const
>     {
>       const_unique_access<int> value(valueGuard);
>       return *value;
>     }
>     void SetValue(int v)
>     {
>       unique_access<int> value(valueGuard);
>       *value = v;
>     }
>   private:
>     unique_access_guard<int> valueGuard;
>   };
>

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:

uniquely_accessed<int> value;
...
value.Access([](const int& i)
{
   ...
});
...
value.Mutate([](int& i)
{
   ...
});

Just as another take on the same construct.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110702/2aec5e23/attachment.html>


More information about the cfe-dev mailing list