|
Click
|
#include <multithread.hh>
Protected Attributes | |
| T | storage [N] |
| volatile int | rcu_current |
unprotected_rcu_singlewriter implements a very simple SINGLE writer, multiple-reader rcu.
rcu allows to do a kind of atomic write with any structure. A temp copy is made for the writer, and when the writer has finished his write, he commits the change by changing the pointer for all subsequent readers.
Unlike usual RCU implementation, this one does not need lock for readers, however memory will be corrupted if N subsequent writes happens while a reader is obtaining a reference. If N cannot be asserted in your use-case, this structure is not for you. Hence, the unprotected in the name to attract attention to this fact.
This works by having a ring of N elements. The writer updates the next element of the ring, and when it has finished, it updates the current ring index to the next element. For fast wrap-up computing, N must be a power of 2 and at least 2.
Using a ring allows for getting rid of atomic reference count, but introduces the N problem.
For this to work, the template class must be able to copy itself with "=", eg : T b; b.do_something(); T a = b; This is true for primitive, but you may need to implement operator= for class
Usage : unprotected_rcu_singlewriter<my_struct,2> rcu_struct; reader : click_chatter("Member is %d.", rcu_struct.read().member); click_chatter("Member is %d.", rcu_struct->member); //Same, more convenient writer : my_struct &tmp_write = rcu_struct->write_begin(); tmp_write->member++; rcu_struct->write_commit();