A synchronized value with a per‐thread cache for accelerated reads.
Synopsis
Declared in <folly/concurrency/ThreadCachedSynchronized.h>
template<
typename T,
typename Mutex = SharedMutex>
class thread_cached_synchronized;
Description
Roughly equivalent to Synchronized, but with a per‐thread cache for acceleration.
Use in hot code when Synchronized alone, with its shared lock and unlock, would be too costly.
Avoid when acceleration is marginal since per‐thread caches are expensive.
Example:
struct writer_and_readers { folly::thread_cached_synchronized<std::shared_ptr<data>> obj_; std::jthread background_writer_{std::bind(loop_update, this)};
std::shared_ptr<data> get_recent_data_fast() { return obj; }
data fetch_recent_data(); bool needs_data_and_not_signaled_done(); void loop_update() { while (needs_data_and_not_signaled_done()) { obj.exchange(folly::copy_to_shared_ptr(fetch_recent_data())); } } };
Note: A singleton variation of this with SingletonThreadLocal would remove one of the branches when looking up the per‐thread cache but would introduce a new branch when looking up the global version.
Type Aliases
Name |
Description |
The stored value type. |
Member Functions
Name |
Description |
|
Constructors |
Assigns a new value to the synchronized store. |
|
Atomically replaces the stored value if it equals the expected value. |
|
Replaces the stored value and returns the previous one. |
|
Returns a copy of the current value. |
|
Returns a const reference to the cached stored value. |
|
Accesses members of the cached stored value. |
|
Replaces the stored value under the lock and invalidates caches. |
|
Swaps the stored value with the given object. |
|
Implicitly converts to a copy of the current value. |
Friends
Name |
Description |
Swaps the stored value of a synchronized object with another object. |
Created with MrDocs