Boost logo

Boost :

From: Gavin Lambert (boost_at_[hidden])
Date: 2022-06-20 23:04:40


On 19/06/2022 07:52, Andrey Semashev wrote:
> I would pass a reference to upgrade_lock as an argument to every
> function that expects it to be locked and wants to upgrade it.
>
> void read_func()
> {
> upgrade_lock l(data_mut);
> read(data);
> if (foo) write_func(l);
> }
>
> void write_func(upgrade_lock& l)
> {
> upgrade_to_unique_lock ll(l);
> mutate(data);
> }
>
> This API is self-documenting, you won't be calling write_func without
> having an upgrade_lock.

Another option (which may be attractive if this is the only place that
write_func is called, and is relatively small so does not benefit much
from being a named method) is simply to inline it using an anonymous
scope block:

     void read_func()
     {
         upgrade_lock l(data_mut);
         read(data);
         {
             upgrade_to_unique_lock ll(l);
             mutate(data);
         }
         read2(data); //back to shared+upgrade lock
     }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk