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 }