Concurrent Queues And the ABA problem

advertisement
Concurrent Queues
And the ABA
problem
Zorik Gekhman
1
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Contents
• Intro
• Unbounded Lock-Free queue
• The ABA problem
• Summary
2
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Contents
• Intro
• Unbounded Lock-Free queue
• The ABA problem
• Summary
3
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Intro
•Queue synchronization:
•deq() & deq()
•enq() & enq()
•enq() & deq() ??
4
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Intro
• Our queue implementation:
head
?
tail
A
?
B
D
C
sentinel
5
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
A
B
C
Contents
• Intro
• Unbounded Lock-Free queue
• The ABA problem
• Summary
6
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Unbounded lock-free queue
•deq() & deq()
•enq() & enq()
7
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Unbounded lock-free queue
8
• How do we enqueue and dequeue without
locks??
• Atomic operations
• deq()
• Takes place in one step
• enq() is lazy
• Takes place in two steps
• Other threads may help complete the
second step
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Intro
• Reminder – deq()
head
?
tail
A
?
B
C
sentinel
9
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
A
B
C
Unbounded lock-free queue
10
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Intro
• Reminder – enq()
head
?
tail
A
B
D
C
sentinel
11
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
A
B
C
Unbounded lock-free queue
null
12
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Unbounded lock-free queue
• Summary
• Less intuitive but more efficient solution
• compareAndSet
• sampling
• Threads help each other (and themselves)
13
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Contents
• Intro
• Unbounded Lock-Free queue
• The ABA problem
• Summary
14
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
a
The ABA problem
b
a
b
a
c
b
15
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
a
b
The ABA problem
• This phenomenon is called the ABA problem
• Shows up especially with conditional
synchronization
16
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
The ABA problem
• How to fix?
• Generally – test not whether value still
same, but whether it has ever changed
• For example - tag each atomic reference
with a unique stamp
17
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
The ABA problem
• get() – returns the object and stores stamp
• CompareAndSet – tests both the stamp and
the reference..
18
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
The ABA problem
19
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
The ABA problem
• Summary
• Often we use recycling garbage collectors
• To avoid the ABA problem:
• instead comparing values – check if value ever
changed
• Can be done using stamps
20
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Contents
• Intro
• Unbounded Lock-Free queue
• The ABA problem
• Summary
21
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Summary
• Lock-free queue
• compareAndSet()
• Sampling
• Helping other theads
• ABA problem
• Check if value ever changed
• stamps
22
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Thank you
23
©”The Art of Multiprocessor programming” by Maurice Herlihy & Nir Shavit
Download