Linked list with concurrent insertions:
Are multiple concurrent insertions guaranteed to eventually succeed?
public class LockedQueue<T> {
int head, tail;
T[] contents;
Lock lock;
...
public void enq(T x) {
lock.lock();
try {
items[tail} = x;
tail++;
} finally {
lock.unlock();
}
}
public T deq() {
lock.lock();
try {
T x = items[head];
head++;
return x;
} finally {
lock.unlock();
}
}
}
What makes this queue implementation easy to reason about?
If multiple threads access the queue
Queues are first-in first-out (FIFO) data structures
What does FIFO even mean if objects can be enqueued concurrently?
Consider:
enq(x)
, enq(y)
, respectivelydeq()
What is expected behavior?
Depends on how concurrent operations are resolved!
Method calls are linearly sorted:
Method calls performed sequentially $\implies$ state well defined between method calls.
“Correct” behavior no longer well defined!
deq()
could return either x
or y
A concurrent execution of a data structure is “correct” if it is consistent with some sequential execution of the data structure.
Response to each method call in concurrent execution is the same as the sequential execution.
Define sensible qualities for how executions should behave:
These are less rigid requirements than being essentially sequential
Consider all method calls made by all threads
Behavior of execution should be consistent with some sequential execution of the method calls.
Behavior of execution should be consistent with some sequential execution of the method calls.
Queue with multiple threads:
enq(1)
then enq(2)
1
or 2
deq()
a bunch of timesShould have:
1
before 2
Method calls should appear to take effect in program order
methodOne()
before methodTwo()
, then methodOne()
should take effect before methodTwo()
in sequential execution.An execution is sequentially consistent if all method calls can be ordered such that:
An implementation of an object is sequentially consistent if
What are possible outcomes of deq()
calls in a sequentially consistent execution?