Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::lockfree::stack< T, A0, A1, A2 > Class Template Reference

The stack class provides a multi-writer/multi-reader stack, pushing and popping is lock-free, construction/destruction has to be synchronized. More...

#include <stack.hpp>

Public Types

typedef T value_type
 
typedef
implementation_defined::allocator 
allocator
 
typedef
implementation_defined::size_type 
size_type
 

Public Member Functions

bool is_lock_free (void) const
 
void reserve (size_type n)
 Allocate n nodes for freelist. More...
 
void reserve_unsafe (size_type n)
 Allocate n nodes for freelist. More...
 
 ~stack (void)
 Destroys stack, free all nodes from freelist. More...
 
bool push (T const &v)
 Pushes object t to the stack. More...
 
bool bounded_push (T const &v)
 Pushes object t to the stack. More...
 
template<typename ConstIterator >
ConstIterator push (ConstIterator begin, ConstIterator end)
 Pushes as many objects from the range [begin, end) as freelist node can be allocated. More...
 
template<typename ConstIterator >
ConstIterator bounded_push (ConstIterator begin, ConstIterator end)
 Pushes as many objects from the range [begin, end) as freelist node can be allocated. More...
 
bool unsynchronized_push (T const &v)
 Pushes object t to the stack. More...
 
template<typename ConstIterator >
ConstIterator unsynchronized_push (ConstIterator begin, ConstIterator end)
 Pushes as many objects from the range [begin, end) as freelist node can be allocated. More...
 
bool pop (T &ret)
 Pops object from stack. More...
 
template<typename U >
bool pop (U &ret)
 Pops object from stack. More...
 
bool unsynchronized_pop (T &ret)
 Pops object from stack. More...
 
template<typename U >
bool unsynchronized_pop (U &ret)
 Pops object from stack. More...
 
template<typename Functor >
bool consume_one (Functor &f)
 consumes one element via a functor More...
 
template<typename Functor >
bool consume_one (Functor const &f)
 consumes one element via a functor More...
 
template<typename Functor >
size_t consume_all (Functor &f)
 consumes all elements via a functor More...
 
template<typename Functor >
size_t consume_all (Functor const &f)
 consumes all elements via a functor More...
 
bool empty (void) const
 
 stack (void)
 Construct stack. More...
 
template<typename U >
 stack (typename node_allocator::template rebind< U >::other const &alloc)
 Construct stack. More...
 
 stack (allocator const &alloc)
 Construct stack. More...
 
 stack (size_type n)
 Construct stack, allocate n nodes for the freelist. More...
 
template<typename U >
 stack (size_type n, typename node_allocator::template rebind< U >::other const &alloc)
 Construct stack, allocate n nodes for the freelist. More...
 

Detailed Description

template<typename T, class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
class boost::lockfree::stack< T, A0, A1, A2 >

The stack class provides a multi-writer/multi-reader stack, pushing and popping is lock-free, construction/destruction has to be synchronized.

It uses a freelist for memory management, freed nodes are pushed to the freelist and not returned to the OS before the stack is destroyed.

Policies:

  • boost::lockfree::fixed_sized<>, defaults to boost::lockfree::fixed_sized<false>
    Can be used to completely disable dynamic memory allocations during push in order to ensure lockfree behavior.
    If the data structure is configured as fixed-sized, the internal nodes are stored inside an array and they are addressed by array indexing. This limits the possible size of the stack to the number of elements that can be addressed by the index type (usually 2**16-2), but on platforms that lack double-width compare-and-exchange instructions, this is the best way to achieve lock-freedom.
  • boost::lockfree::capacity<>, optional
    If this template argument is passed to the options, the size of the stack is set at compile-time.
    It this option implies fixed_sized<true>
  • boost::lockfree::allocator<>, defaults to boost::lockfree::allocator<std::allocator<void>>
    Specifies the allocator that is used for the internal freelist

Requirements:

  • T must have a copy constructor

Member Typedef Documentation

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
typedef implementation_defined::allocator boost::lockfree::stack< T, A0, A1, A2 >::allocator
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
typedef implementation_defined::size_type boost::lockfree::stack< T, A0, A1, A2 >::size_type
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
typedef T boost::lockfree::stack< T, A0, A1, A2 >::value_type

Constructor & Destructor Documentation

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
boost::lockfree::stack< T, A0, A1, A2 >::stack ( void  )
inline

Construct stack.

References BOOST_ASSERT.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename U >
boost::lockfree::stack< T, A0, A1, A2 >::stack ( typename node_allocator::template rebind< U >::other const &  alloc)
inlineexplicit

Construct stack.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
boost::lockfree::stack< T, A0, A1, A2 >::stack ( allocator const &  alloc)
inlineexplicit

Construct stack.

References BOOST_ASSERT.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
boost::lockfree::stack< T, A0, A1, A2 >::stack ( size_type  n)
inlineexplicit

Construct stack, allocate n nodes for the freelist.

References BOOST_ASSERT.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename U >
boost::lockfree::stack< T, A0, A1, A2 >::stack ( size_type  n,
typename node_allocator::template rebind< U >::other const &  alloc 
)
inline

Construct stack, allocate n nodes for the freelist.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
boost::lockfree::stack< T, A0, A1, A2 >::~stack ( void  )
inline

Destroys stack, free all nodes from freelist.

Note
not thread-safe

References boost::lockfree::stack< T, A0, A1, A2 >::unsynchronized_pop().

Member Function Documentation

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
bool boost::lockfree::stack< T, A0, A1, A2 >::bounded_push ( T const &  v)
inline

Pushes object t to the stack.

Postcondition
object will be pushed to the stack, if internal node can be allocated
Returns
true, if the push operation is successful.
Note
Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename ConstIterator >
ConstIterator boost::lockfree::stack< T, A0, A1, A2 >::bounded_push ( ConstIterator  begin,
ConstIterator  end 
)
inline

Pushes as many objects from the range [begin, end) as freelist node can be allocated.

Returns
iterator to the first element, which has not been pushed
Note
Operation is applied atomically
Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
Exceptions
ifmemory allocator throws

References boost::asio::begin, and boost::end.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename Functor >
size_t boost::lockfree::stack< T, A0, A1, A2 >::consume_all ( Functor &  f)
inline

consumes all elements via a functor

sequentially pops all elements from the stack and applies the functor on each object

Returns
number of elements that are consumed
Note
Thread-safe and non-blocking, if functor is thread-safe and non-blocking

References boost::lockfree::stack< T, A0, A1, A2 >::consume_one().

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename Functor >
size_t boost::lockfree::stack< T, A0, A1, A2 >::consume_all ( Functor const &  f)
inline

consumes all elements via a functor

sequentially pops all elements from the stack and applies the functor on each object

Returns
number of elements that are consumed
Note
Thread-safe and non-blocking, if functor is thread-safe and non-blocking

References boost::lockfree::stack< T, A0, A1, A2 >::consume_one().

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename Functor >
bool boost::lockfree::stack< T, A0, A1, A2 >::consume_one ( Functor &  f)
inline

consumes one element via a functor

pops one element from the stack and applies the functor on this object

Returns
true, if one element was consumed
Note
Thread-safe and non-blocking, if functor is thread-safe and non-blocking

References boost::memory_order_consume.

Referenced by boost::lockfree::stack< T, A0, A1, A2 >::consume_all(), and boost::lockfree::stack< T, A0, A1, A2 >::pop().

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename Functor >
bool boost::lockfree::stack< T, A0, A1, A2 >::consume_one ( Functor const &  f)
inline

consumes one element via a functor

pops one element from the stack and applies the functor on this object

Returns
true, if one element was consumed
Note
Thread-safe and non-blocking, if functor is thread-safe and non-blocking

References boost::memory_order_consume.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
bool boost::lockfree::stack< T, A0, A1, A2 >::empty ( void  ) const
inline
Returns
true, if stack is empty.
Note
It only guarantees that at some point during the execution of the function the stack has been empty. It is rarely practical to use this value in program logic, because the stack can be modified by other threads.
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
bool boost::lockfree::stack< T, A0, A1, A2 >::is_lock_free ( void  ) const
inline
Returns
true, if implementation is lock-free.
Warning
It only checks, if the top stack node and the freelist can be modified in a lock-free manner. On most platforms, the whole implementation is lock-free, if this is true. Using c++0x-style atomics, there is no possibility to provide a completely accurate implementation, because one would need to test every internal node, which is impossible if further nodes will be allocated from the operating system.
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
bool boost::lockfree::stack< T, A0, A1, A2 >::pop ( T ret)
inline

Pops object from stack.

Postcondition
if pop operation is successful, object will be copied to ret.
Returns
true, if the pop operation is successful, false if stack was empty.
Note
Thread-safe and non-blocking
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename U >
bool boost::lockfree::stack< T, A0, A1, A2 >::pop ( U &  ret)
inline

Pops object from stack.

Precondition
type T must be convertible to U
Postcondition
if pop operation is successful, object will be copied to ret.
Returns
true, if the pop operation is successful, false if stack was empty.
Note
Thread-safe and non-blocking

References boost::lockfree::stack< T, A0, A1, A2 >::consume_one(), and boost::program_options::value().

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
bool boost::lockfree::stack< T, A0, A1, A2 >::push ( T const &  v)
inline

Pushes object t to the stack.

Postcondition
object will be pushed to the stack, if internal node can be allocated
Returns
true, if the push operation is successful.
Note
Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated from the OS. This may not be lock-free.
Exceptions
ifmemory allocator throws
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename ConstIterator >
ConstIterator boost::lockfree::stack< T, A0, A1, A2 >::push ( ConstIterator  begin,
ConstIterator  end 
)
inline

Pushes as many objects from the range [begin, end) as freelist node can be allocated.

Returns
iterator to the first element, which has not been pushed
Note
Operation is applied atomically
Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated from the OS. This may not be lock-free.
Exceptions
ifmemory allocator throws

References boost::asio::begin, and boost::end.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
void boost::lockfree::stack< T, A0, A1, A2 >::reserve ( size_type  n)
inline

Allocate n nodes for freelist.

Precondition
only valid if no capacity<> argument given
Note
thread-safe, may block if memory allocator blocks

References boost::n.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
void boost::lockfree::stack< T, A0, A1, A2 >::reserve_unsafe ( size_type  n)
inline

Allocate n nodes for freelist.

Precondition
only valid if no capacity<> argument given
Note
not thread-safe, may block if memory allocator blocks

References boost::n.

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
bool boost::lockfree::stack< T, A0, A1, A2 >::unsynchronized_pop ( T ret)
inline

Pops object from stack.

Postcondition
if pop operation is successful, object will be copied to ret.
Returns
true, if the pop operation is successful, false if stack was empty.
Note
Not thread-safe, but non-blocking

Referenced by boost::lockfree::stack< T, A0, A1, A2 >::~stack().

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename U >
bool boost::lockfree::stack< T, A0, A1, A2 >::unsynchronized_pop ( U &  ret)
inline

Pops object from stack.

Precondition
type T must be convertible to U
Postcondition
if pop operation is successful, object will be copied to ret.
Returns
true, if the pop operation is successful, false if stack was empty.
Note
Not thread-safe, but non-blocking

References boost::memory_order_relaxed, and boost::program_options::value().

template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
bool boost::lockfree::stack< T, A0, A1, A2 >::unsynchronized_push ( T const &  v)
inline

Pushes object t to the stack.

Postcondition
object will be pushed to the stack, if internal node can be allocated
Returns
true, if the push operation is successful.
Note
Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated from the OS. This may not be lock-free.
Exceptions
ifmemory allocator throws
template<typename T , class A0 = boost::parameter::void_, class A1 = boost::parameter::void_, class A2 = boost::parameter::void_>
template<typename ConstIterator >
ConstIterator boost::lockfree::stack< T, A0, A1, A2 >::unsynchronized_push ( ConstIterator  begin,
ConstIterator  end 
)
inline

Pushes as many objects from the range [begin, end) as freelist node can be allocated.

Returns
iterator to the first element, which has not been pushed
Note
Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated from the OS. This may not be lock-free.
Exceptions
ifmemory allocator throws

References boost::asio::begin, and boost::end.


The documentation for this class was generated from the following file: