Re:Lock-free Data Structures Part 1 – Introduction
by iano on Tuesday May 06, 2014 @01:01AM
(#7702917)
Can someone explain to me how the first queue example's (using singly link lists) dequeue method gives the expected result? Isn't head->m_pNext always NULL, so the new head will always be NULL and tail will be set to NULL losing the rest of the queue?
Seems like you should need to traverse the list to dequeue.
Re:Lock-free Data Structures Part 1 – Introduction
by recentdarkness on Tuesday May 06, 2014 @03:29AM
(#7703215)
I have to say that even though it is nice to write about lock free algorithms, it would be probably worth noting that this implementation is dismissing the problem of CPU caches which eventually and most likely lead to race conditions.
Re:Lock-free Data Structures Part 1 – Introduction
by augustl on Tuesday May 06, 2014 @03:32AM
(#7703221)
What kind of race conditions are you talking about? Something along the lines of memory access that causes the CPU caches to be purged a lot more than they normally would? Genuine question, I know little about CPU cache internals :)
Re:Lock-free Data Structures Part 1 – Introduction
by pkolaczk on Tuesday May 06, 2014 @06:34AM
(#7703622)
I don't know if this implementation got it right (I don't have a spare month to prove/disprove its correctness)but my advice would be: be very, very careful with lockfree code in C++. This the area where dragons live.
Let me cite Herb Sutter:
"Second, it's hard even for experts. It's easy to write lock-free code that appears to work, but it's very difficult to write lock-free code that is correct and performs well. Even good magazines and refereed journals have published a substantial amount of lock-free code that was actually broken in subtle ways and needed correction.
To illustrate, let's dissect some peer-reviewed lock-free code that was published here in DDJ just two months ago [2]. The author, Petru Marginean, has graciously allowed me to dissect it here so that we can see what's wrong and why, what lessons we should learn, and how to write the code correctly. That someone as knowledgable as Petru, who has published many good and solid articles, can get this stuff wrong should be warning enough that lock-free coding requires great care."
Re:Lock-free Data Structures Part 1 – Introduction
by brigade on Tuesday May 06, 2014 @10:49AM
(#7704794)
Cache coherency isn't something that average app developers have had to worry about since forever - the only place I can think of most developers might encounter issues is with GPU programming, but graphics APIs tend to deal with coherency issues pretty well.
Re:Lock-free Data Structures Part 1 – Introduction
by colanderman on Tuesday May 06, 2014 @04:07PM
(#7706778)
Jacques said "consistency", not "coherency"… those are different things.
Yes, most processors provide a coherent cache – individual writes are guaranteed to be eventually seen by all cores. However, most modern processors do not provide a consistent cache (for performance's sake): the order that unrelated writes become visible is not guaranteed.
Memory fences (which are not flushes or invalidates; neither implies the other!) instruct the CPU to provide such ordering.
Seems like you should need to traverse the list to dequeue.
Where did I go wrong?
The solution is memory barriers:
http://en.wikipedia.org/wiki/Memory_barrier
That's the point of this post (well, the second I guess) - the lock free version has those via atomics.
edit: reading the http://en.cppreference.com/w/cpp/atomic/memory_order there is tons of data on re-ordering but nothing on cache consistency so I am assuming that's a job farmed out to the hardware somehow.
In that case ignore above comment :)
Let me cite Herb Sutter: "Second, it's hard even for experts. It's easy to write lock-free code that appears to work, but it's very difficult to write lock-free code that is correct and performs well. Even good magazines and refereed journals have published a substantial amount of lock-free code that was actually broken in subtle ways and needed correction.
To illustrate, let's dissect some peer-reviewed lock-free code that was published here in DDJ just two months ago [2]. The author, Petru Marginean, has graciously allowed me to dissect it here so that we can see what's wrong and why, what lessons we should learn, and how to write the code correctly. That someone as knowledgable as Petru, who has published many good and solid articles, can get this stuff wrong should be warning enough that lock-free coding requires great care."
http://www.drdobbs.com/cpp/lock-free-code-a-false-sense-of-s...
P.D: Weird github use:
https://github.com/liblfds?tab=repositories
Yes, most processors provide a coherent cache – individual writes are guaranteed to be eventually seen by all cores. However, most modern processors do not provide a consistent cache (for performance's sake): the order that unrelated writes become visible is not guaranteed.
Memory fences (which are not flushes or invalidates; neither implies the other!) instruct the CPU to provide such ordering.
http://webcache.googleusercontent.com/search?q=cache:kgO8-Tc...