folly::Synchronized

folly::Synchronized pairs a datum with a mutex. The datum can only be reached through a LockedPtr, typically acquired via .rlock() or .wlock(); the mutex is held for the lifetime of the LockedPtr.

Synopsis

Declared in <folly/Synchronized.h>

template<
    class T,
    class Mutex = SharedMutex>
struct Synchronized
    : SynchronizedBase<Synchronized<T, Mutex>, detail::kSynchronizedMutexLevel<Mutex>>

Description

It is recommended to explicitly open a new nested scope when aquiring a LockedPtr object, to help visibly delineate the critical section and to ensure that the LockedPtr is destroyed as soon as it is no longer needed.

refcode folly/docs/examples/folly/Synchronized.cpp

Base Classes

NameDescription
SynchronizedBase<Synchronized<T, Mutex>, detail::kSynchronizedMutexLevel<Mutex>>SynchronizedBase is a helper parent class for Synchronized<T>.

Type Aliases

NameDescription
ConstLockedPtr Pointer type that holds a shared lock on the datum.
DataType The type of the guarded datum.
LockedPtr Pointer type that holds an exclusive lock on the datum.
MutexType The type of the mutex guarding the datum.

Member Functions

NameDescription
Synchronized [constructor]Constructors
operator= Assignment operators
contextualLock contextualLock overloads
contextualRLock contextualRLock overloads
copy Returns a fresh copy of the datum.
copyInto Copies datum to a given target.
exchange Exchange datum.
swap swap overloads
timedAcquire timedAcquire overloads
unsafeGetMutex Access underlying mutex_ directly.
unsafeGetUnlocked unsafeGetUnlocked overloads

Friends

NameDescription
folly::LockedPtrA LockedPtr keeps a Synchronized<T> object locked for the duration of LockedPtr's existence.
folly::LockedPtrBaseBase class that owns the lock held by a LockedPtr.

Non-Member Functions

NameDescription
lockMake an exclusive locking helper for a const object.
lockMake an exclusive locking helper.
rlockMake a shared locking helper for a const object.
swapSwap the data held by two Synchronized objects.
ulockMake an upgrade locking helper.
wlockMake an exclusive locking helper for a const object.
wlockHelper functions that should be passed to either a lock() or synchronized() invocation, these return implementation defined structs that will be used to lock the synchronized instance appropriately.

Derived Classes

NameDescription
ImplicitSynchronized [deprecated]Deprecated subclass of Synchronized that provides implicit locking via operator->. This is intended to ease migration while preventing accidental use of operator-> in new code.

Template Parameters

NameDescription
TThe type of datum to be stored.
MutexThe mutex type that guards the datum. Must be Lockable.