1

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.

  • Why is `folly:Synchronized` speical? If it isnt broken then it should be like with other types too: You don't need to call a non-const method, then it can be `const`, otherwise it needs to be non-const. – 463035818_is_not_an_ai Jan 23 '23 at 19:43
  • Since you already found the source code, why don't you look at those two return types? https://github.com/facebook/folly/blob/a9a982428c495fe91b0f8f9dd1486219b0d76dbc/folly/Synchronized.h#L168 – tkausl Jan 23 '23 at 19:44
  • the `wlock` overload that is const does not actually grant you write access. https://github.com/facebook/folly/blob/56dc2459f45159370cfc2e628c6172125d906917/folly/Synchronized.h (line 197) – 463035818_is_not_an_ai Jan 23 '23 at 19:51
  • @tkausl I understand that the types are different but what I'm interested in is any difference in functionality. – Chris Gregory Jan 23 '23 at 19:51
  • I think you will understand when you try and see what happens – 463035818_is_not_an_ai Jan 23 '23 at 19:52
  • The point I was trying to make is that they are __not__ different. These two are aliases to the exact same class, the only difference is the held item being `const`. – tkausl Jan 23 '23 at 19:52

1 Answers1

0

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.