Increasing the reference counter can always be done with memory_order_relaxed:
New references to an object can only be formed from an existing reference,
and passing an existing reference from one thread to another must already
provide any required synchronization.

It is important to enforce any possible access to the object in one thread
(through an existing reference) to happen before deleting
the object in a different thread. This is achieved by a "release"
operation after dropping a reference (any access to the object through
this reference must obviously happened before), and an "acquire"
operation before deleting the object.

It would be possible to use memory_order_acq_rel for
the fetch_sub operation, but this results in unneeded
"acquire" operations when the reference counter does not yet
reach zero and may impose a performance penalty.

The purpose of a spin lock is to prevent multiple threads
from concurrently accessing a shared data structure. In contrast to a mutex,
threads will busy-wait and waste CPU cycles instead of yielding the CPU to
another thread. Do not use spinlocks unless you are certain that
you understand the consequences.

The purpose of the spinlock is to make sure that one access to the shared
data structure always strictly "happens before" another. The
usage of acquire/release in lock/unlock is required and sufficient to guarantee
this ordering.

It would be correct to write the "lock" operation in the following
way:

This "optimization" is however a) useless and b) may in fact
hurt: a) Since the thread will be busily spinning on a blocked spinlock,
it does not matter if it will waste the CPU cycles with just "exchange"
operations or with both useless "exchange" and "acquire"
operations. b) A tight "exchange" loop without any memory-synchronizing
instruction introduced through an "acquire" operation will on
some systems monopolize the memory subsystem and degrade the performance
of other system components.

The purpose of the Singleton with double-checked locking pattern
is to ensure that at most one instance of a particular object is created.
If one instance has been created already, access to the existing object should
be as light-weight as possible.

The mutex makes sure that only one instance of the object is ever created.
The instance method must make sure that any dereference
of the object strictly "happens after" creating the instance
in another thread. The use of memory_order_release after
creating and initializing the object and memory_order_consume
before dereferencing the object provides this guarantee.

It would be permissible to use memory_order_acquire
instead of memory_order_consume, but this provides a
stronger guarantee than is required since only operations depending on
the value of the pointer need to be ordered.

A wait-free ring buffer provides a mechanism for relaying
objects from one single "producer" thread to one single "consumer"
thread without any locks. The operations on this data structure are "wait-free"
which means that each operation finishes within a constant number of steps.
This makes this data structure suitable for use in hard real-time systems
or for communication with interrupt/signal handlers.

The implementation makes sure that the ring indices do not "lap-around"
each other to ensure that no elements are either lost or read twice.

Furthermore it must guarantee that read-access to a particular object in
pop "happens after" it has been written in
push. This is achieved by writing head_
with "release" and reading it with "acquire". Conversely
the implementation also ensures that read access to a particular ring element
"happens before" before rewriting this element with a new value
by accessing tail_ with appropriate ordering constraints.

template<typenameT>classwaitfree_queue{public:structnode{Tdata;node*next;};voidpush(constT&data){node*n=newnode;n->data=data;node*stale_head=head_.load(boost::memory_order_relaxed);do{n->next=stale_head;}while(!head_.compare_exchange_weak(stale_head,n,boost::memory_order_release));}node*pop_all(void){T*last=pop_all_reverse(),*first=0;while(last){T*tmp=last;last=last->next;tmp->next=first;first=tmp;}returnfirst;}waitfree_queue():head_(0){}// alternative interface if ordering is of no importancenode*pop_all_reverse(void){returnhead_.exchange(0,boost::memory_order_consume);}private:boost::atomic<node*>head_;};

The implementation guarantees that all objects enqueued are processed in
the order they were enqueued by building a singly-linked list of object
in reverse processing order. The queue is atomically emptied by the consumer
and brought into correct order.

It must be guaranteed that any access to an object to be enqueued by the
producer "happens before" any access by the consumer. This is
assured by inserting objects into the list with release
and dequeuing them with consume memory order. It is
not necessary to use acquire memory order in waitfree_queue::pop_all
because all operations involved depend on the value of the atomic pointer
through dereference