7 ms·
folly::Synchronized<T> provides a similar pattern. It works well for very simplistic concurrency schemes. struct { int a; int b; } A; folly::Synchronized
by loeg 4y ago
folly::Synchronized<T> provides a similar pattern. It works well for very simplistic concurrency schemes.
struct { int a; int b; } A;
folly::Synchronized<A> syncA;
int c;
{
auto aReader = syncA.rlock();
c = aReader->a;
// aReader going out of scope drops the rlock
}
{
auto aWriter = syncA.wlock();
aWriter->b = 15;
// aWriter going out of scope drops the wlock
}
I would argue that the pseudo-pointer accessor objects are more usable than passing lambdas to a `with()` method, but if you want that, folly::Synchronized provides similar `withWLock()` and `withRLock()` methods.
And in fact, MutexProtected seems to provide APIs like Synchronized's `rlock()`/`wlock()`: `lock_shared()` and `lock_exclusive()`: https://github.com/SerenityOS/serenity/blob/master/Kernel/Locking/MutexProtected.h#L58-L59 https://github.com/SerenityOS/serenity/blob/master/Kernel/Lo... I don't know why the article doesn't touch on these.
https://github.com/facebook/folly/blob/main/folly/docs/Synchronized.md https://github.com/facebook/folly/blob/main/folly/docs/Synch...
https://github.com/facebook/folly/blob/main/folly/Synchronized.h https://github.com/facebook/folly/blob/main/folly/Synchroniz...