On Sat, Nov 21, 2015 at 2:15 AM, Rajinder Yadav <rajind...@hotmail.com> wrote:
> Hello Dmitry,
>
> I would like to thank you for your excellent write up on lock-free
> programming. I have a question and was wondering if you might be able to
> explain how I might be able to update multiple variables using a lock-less
> design?
>
> For example say I have a linked linked list and I want to be able to
> add/remove nodes and at the same time I want to keep a count on the size of
> the list.
>
> Would you be able to show me a code snippet in C++11 that would add a new
> node at the front of the list and update the head pointer and also increment
> the count. I find it confusing how to go about doing this. I imagine I would
> need to to make use of CAS loop.


Hello Rajinder,

There is so-called MCAS (multi-word compare-and-swap) operation that
allows to atomically update several memory locations:
http://www.mscs.mu.edu/~brylow/SPLASH-MARC-2013/Feldman.pdf
and there is also HTM/STM (hardware/software transactional memory):
https://en.wikipedia.org/wiki/Software_transactional_memory
However, that's probably overkill in your case (both performance- and
complexity-wise), because these techniques are required only when you
need strong atomicity guarantees.

You probably can just modify the list (using whatever algorithm you
are using now) and then increment the size. Or vise versa: increment
the size and then insert into the list, depending on whether it is
more preferable to observe larger or smaller size that it actually is.
If you increment after insertion, then you can also observe negative
size. Most likely negative size can be just truncated to zero.

Here is C++11 snippet for this:

std::atomic<int> size_;
...
size_.fetch_add(1, std::memory_order_relaxed);

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Scalable Synchronization Algorithms" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to lock-free+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/lock-free/CAEeQi3s2PeLWsBJFGRaorBVDufS-1MdKOvY3D%2BFzHmsDV_oEnQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to