c++synchronizationlockingfolly

What does it mean for folly::Synchronized object to be const?


I want to write some utility functions that operate on Synchronized<T> objects. I could write 2 versions one for const and one for non-const but that seems silly. Alternatively I could write 1 version just for const. I'm not sure if there's any downside to this because it seems like you can still modify the "locked" object via Synchronized::wlock method.

folly::Synchronized allows calling wlock (ie lock exclusively in write mode) on both a const Synchronized and non-const Synchronized object -- link.

However they return different output types. Is there any difference between LockedPtr and ConstWLockedPtr? In particular I'm interested in differences in functionality & in particular any lack of functionality with ConstWLockedPtr.


Solution

  • wlock does not compile when used on a const folly::Synchronized<T>&. So you must a mutable reference to call wlock. For rlock you can use mutable or const; it'll just implicitly convert to const.