Note: Please mark this answer as correct only if you actually test this approach.
About your question whether the below struct be safely inserted and removed from lock free container:
struct person
{
string name;
uint32_t age;
}
Multi-byte sequences of any length can be safely inserted/removed from lock free container if you use a redundant encoding. Let us assume that we already have atomic instructions for manipulating 4 bytes at a time (32 bits). In that case, we can encode the uint32_t age
field like so:
struct age_t
{
uint32_t age_low;
uint32_t age_high;
}
The field age_low
stores the low bits (for example, the low 16 bits) of the 32-bit uint32_t age
. The field age_high
stores the remaining high bits. Conceptually:
struct age_t
{
uint16_t age_low;
uint16_t id_low;
uint16_t age_high;
uint16_t id_high;
}
The fields id_low
and id_high
should contain an ID identifying the writer.
A read is implemented as two atomic 32-bit reads, and succeeds if all the id_
parts are equivalent to each other. If it fails, the read operation needs to be restarted.
A write is implemented as two atomic 32-bit writes, and is followed by a read of the whole age_t
value. The write succeeds if: the read mentioned in the previous sentence succeeds and the IDs which were read are equivalent to the written IDs.
About the string
value: the principle is the same. You just need to figure out how to split its binary value similarly to how age
value was split. Same in respect to reading/writing the whole person
structure.