A synchronized value with a per-thread cache for accelerated reads.
Declared in <folly/concurrency/ThreadCachedSynchronized.h>
template<
typename T,
typename Mutex = SharedMutex>
class thread_cached_synchronized;
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.
| Name | Description |
|---|---|
value_type | The stored value type. |
| Name | Description |
|---|---|
thread_cached_synchronized [constructor] | Constructors |
operator= | Assigns a new value to the synchronized store. |
compare_exchange | Atomically replaces the stored value if it equals the expected value. |
exchange | Replaces the stored value and returns the previous one. |
load | Returns a copy of the current value. |
operator* | Returns a const reference to the cached stored value. |
operator-> | Accesses members of the cached stored value. |
store | Replaces the stored value under the lock and invalidates caches. |
swap | Swaps the stored value with the given object. |
operator value_type | Implicitly converts to a copy of the current value. |
| Name | Description |
|---|---|
folly::swap | Swaps the stored value of a synchronized object with another object. |