template<typename T, typename RcuTraits>
class rcu::Variable< T, RcuTraits >
Read-Copy-Update variable.
- See also
- Based on ideas from http://www.drdobbs.com/lock-free-data-structures-with-hazard-po/184401890 with modified API.
A variable with MT-access pattern "very often reads, seldom writes". It is specially optimized for reads. On read, one obtains a ReaderPtr<T> from it and uses the obtained value as long as it wants to. On write, one obtains a WritablePtr<T> with a copy of the last version of the value, makes some changes to it, and commits the result to update current variable value (does Read-Copy-Update). Old version of the value is not freed on update, it will be eventually freed when a subsequent writer identifies that nobody works with this version.
- Note
- There is no way to create a "null" Variable.
Example usage:
constexpr int kOldValue = 1;
constexpr auto kOldString = "Old string";
constexpr int kNewValue = 2;
constexpr auto kNewString = "New string";
struct Data {
int x;
std::string s;
};
{
auto ro_ptr = data.
Read();
ASSERT_EQ(ro_ptr->x, kOldValue);
ASSERT_EQ(ro_ptr->s, kOldString);
}
{
ptr->x = kNewValue;
ptr->s = kNewString;
ptr.Commit();
}
- See also
- Synchronization Primitives
- Template Parameters
-
- Examples
- samples/config_service/main.cpp.
Definition at line 408 of file rcu.hpp.
|
| template<typename... Args> |
| | Variable (Args &&... initial_value_args) |
| | Create a new Variable with an in-place constructed initial value.
|
|
| Variable (const Variable &)=delete |
|
| Variable (Variable &&)=delete |
|
Variable & | operator= (const Variable &)=delete |
|
Variable & | operator= (Variable &&)=delete |
| ReadablePtr< T, RcuTraits > | Read () const |
| | Obtain a smart pointer which can be used to read the current value.
|
| T | ReadCopy () const |
| | Obtain a copy of contained value.
|
| WritablePtr< T, RcuTraits > | StartWrite () |
| template<typename... Args> |
| WritablePtr< T, RcuTraits > | StartWriteEmplace (Args &&... args) |
| void | Assign (T new_value) |
| | Replaces the Variable's value with the provided one.
|
| template<typename... Args> |
| void | Emplace (Args &&... args) |
| | Replaces the Variable's value with an in-place constructed one.
|
| void | Cleanup () |