Additional data structures, synchronization primitives and tools for
concurrent programming. Similiar to Java's java.util.concurrent
package.
sb-concurrency:queue
is a lock-free, thread-safe FIFO queue
datatype.
The implementation is based on An Optimistic Approach to
Lock-Free FIFO Queues by Edya Ladan-Mozes and Nir Shavit.
Before SBCL 1.0.38, this implementation resided in its own contrib
(see sb-queue) which is still provided for backwards-compatibility
but which has since been deprecated.
enqueue
can be used to add objects to a queue, and
dequeue
retrieves items from a queue in FIFO order.
Class precedence list:
queue, structure-object, t
Lock-free thread safe queue.
Retrieves the oldest value in
queue
and returns it as the primary value, andt
as secondary value. If the queue is empty, returnsnil
as both primary and secondary value.
Returns the contents of
queue
as a list without removing them from thequeue
. Mainly useful for manual examination of queue state.
Returns a new
queue
withname
and contents of theinitial-contents
sequence enqueued.
Returns the number of objects in
queue
. Mainly useful for manual examination of queue state, and inprint-object
methods: inefficient as it walks the entire queue.
sb-concurrency:mailbox
is a lock-free message queue where one
or multiple ends can send messages to one or multiple receivers. The
difference to Section sb-concurrency:queue is that the receiving
end may block until a message arrives.
The implementation is based on the Queue implementation above
(see Structure sb-concurrency:queue.)
send-message
can be used to send a message to a mailbox, and
receive-message
retrieves a message from a mailbox, or blocks
until a new message arrives. receive-message-no-hang
is the
non-blocking variant.
Messages can be any object.
Class precedence list:
mailbox, structure-object, t
Mailbox aka message queue.
Returns a fresh list containing all the messages in the mailbox. Does not remove messages from the mailbox.
Returns a new
mailbox
with messages ininitial-contents
enqueued.
Removes the oldest message from
mailbox
and returns it as the primary value. Ifmailbox
is empty waits until a message arrives.
The non-blocking variant of
receive-message
. Returns two values, the message removed frommailbox
, and a flag specifying whether a message could be received.
Removes and returns all (or at most N) currently pending messages from
mailbox
, or returnsnil
if no messages are pending.Note: Concurrent threads may be snarfing messages during the run of this function, so even though
x
,y
appear right next to each other in the result, does not necessarily mean thaty
was the message sent right afterx
.