STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends | List of all members
Concurrency::concurrent_queue< _Ty, _Ax > Class Template Reference

The concurrent_queue class is a sequence container class that allows first-in, first-out access to its elements. It enables a limited set of concurrency-safe operations, such as push and try_pop. More...

#include <concurrent_queue.h>

Inheritance diagram for Concurrency::concurrent_queue< _Ty, _Ax >:
Concurrency::details::_Concurrent_queue_base_v4

Classes

class  _Destroyer
 

Public Types

typedef _Ty value_type
 A type that represents the data type stored in a concurrent queue. More...
 
typedef _Ax allocator_type
 A type that represents the allocator class for the concurrent queue. More...
 
typedef _Ty & reference
 A type that provides a reference to an element stored in a concurrent queue. More...
 
typedef const _Ty & const_reference
 A type that provides a reference to a const element stored in a concurrent queue for reading and performing const operations. More...
 
typedef std::size_t size_type
 A type that counts the number of elements in a concurrent queue. More...
 
typedef std::ptrdiff_t difference_type
 A type that provides the signed distance between two elements in a concurrent queue. More...
 
typedef details::_Concurrent_queue_iterator< concurrent_queue, _Ty > iterator
 A type that represents a non-thread-safe iterator over the elements in a concurrent queue. More...
 
typedef details::_Concurrent_queue_iterator< concurrent_queue, const _Ty > const_iterator
 A type that represents a non-thread-safe const iterator over elements in a concurrent queue. More...
 

Public Member Functions

 concurrent_queue (const allocator_type &_Al=allocator_type())
 Constructs a concurrent queue. More...
 
 concurrent_queue (const concurrent_queue &_OtherQ, const allocator_type &_Al=allocator_type())
 Constructs a concurrent queue. More...
 
 concurrent_queue (concurrent_queue &&_OtherQ, const allocator_type &_Al=allocator_type())
 Constructs a concurrent queue. More...
 
template<typename _InputIterator >
 concurrent_queue (_InputIterator _Begin, _InputIterator _End)
 Constructs a concurrent queue. More...
 
 ~concurrent_queue ()
 Destroys the concurrent queue. More...
 
void push (const _Ty &_Src)
 Enqueues an item at tail end of the concurrent queue. This method is concurrency-safe. More...
 
void push (_Ty &&_Src)
 Enqueues an item at tail end of the concurrent queue. This method is concurrency-safe. More...
 
bool try_pop (_Ty &_Dest)
 Dequeues an item from the queue if one is available. This method is concurrency-safe. More...
 
size_type unsafe_size () const
 Returns the number of items in the queue. This method is not concurrency-safe. More...
 
bool empty () const
 Tests if the concurrent queue is empty at the moment this method is called. This method is concurrency-safe. More...
 
allocator_type get_allocator () const
 Returns a copy of the allocator used to construct the concurrent queue. This method is concurrency-safe. More...
 
void clear ()
 Clears the concurrent queue, destroying any currently enqueued elements. This method is not concurrency-safe. More...
 
iterator unsafe_begin ()
 Returns an iterator of type iterator or const_iterator to the beginning of the concurrent queue. This method is not concurrency-safe. More...
 
iterator unsafe_end ()
 Returns an iterator of type iterator or const_iterator to the end of the concurrent queue. This method is not concurrency-safe. More...
 
const_iterator unsafe_begin () const
 Returns an iterator of type iterator or const_iterator to the beginning of the concurrent queue. This method is not concurrency-safe. More...
 
const_iterator unsafe_end () const
 Returns an iterator of type iterator or const_iterator to the end of the concurrent queue. This method is not concurrency-safe. More...
 

Private Types

typedef _Ax::template rebind< char >::other _Page_allocator_type
 

Private Member Functions

_Ty & _Get_ref (_Page &_Pg, size_t _Index)
 
virtual void _Copy_item (_Page &_Dst, size_t _Index, const void *_Src)
 
virtual void _Move_item (_Page &_Dst, size_t _Index, void *_Src)
 
virtual void _Assign_and_destroy_item (void *_Dst, _Page &_Src, size_t _Index)
 
virtual _Page_Allocate_page ()
 
virtual void _Deallocate_page (_Page *_Pg)
 

Private Attributes

_Page_allocator_type _My_allocator
 

Friends

template<typename _Container , typename _Value >
class ::Concurrency::details::_Concurrent_queue_iterator
 

Additional Inherited Members

- Protected Member Functions inherited from Concurrency::details::_Concurrent_queue_base_v4
_CONCRTIMP _Concurrent_queue_base_v4 (size_t _Item_size)
 
virtual _CONCRTIMP ~_Concurrent_queue_base_v4 ()
 
_CONCRTIMP void _Internal_push (const void *_Src)
 
_CONCRTIMP void _Internal_move_push (void *_Src)
 
_CONCRTIMP void _Internal_swap (_Concurrent_queue_base_v4 &other)
 
_CONCRTIMP bool _Internal_pop_if_present (void *_Dst)
 
_CONCRTIMP size_t _Internal_size () const
 
_CONCRTIMP bool _Internal_empty () const
 
_CONCRTIMP void _Internal_finish_clear ()
 
_CONCRTIMP void _Internal_throw_exception () const
 
- Protected Attributes inherited from Concurrency::details::_Concurrent_queue_base_v4
size_t _Items_per_page
 
size_t _Item_size
 

Detailed Description

template<typename _Ty, class _Ax>
class Concurrency::concurrent_queue< _Ty, _Ax >

The concurrent_queue class is a sequence container class that allows first-in, first-out access to its elements. It enables a limited set of concurrency-safe operations, such as push and try_pop.

Template Parameters
_TyThe data type of the elements to be stored in the queue.
_AxThe type that represents the stored allocator object that encapsulates details about the allocation and deallocation of memory for this concurrent queue. This argument is optional and the default value is allocator<_Ty >.

For more information, see Parallel Containers and Objects.

Member Typedef Documentation

template<typename _Ty, class _Ax >
typedef _Ax::template rebind<char>::other Concurrency::concurrent_queue< _Ty, _Ax >::_Page_allocator_type
private
template<typename _Ty, class _Ax >
typedef _Ax Concurrency::concurrent_queue< _Ty, _Ax >::allocator_type

A type that represents the allocator class for the concurrent queue.

template<typename _Ty, class _Ax >
typedef details::_Concurrent_queue_iterator<concurrent_queue,const _Ty> Concurrency::concurrent_queue< _Ty, _Ax >::const_iterator

A type that represents a non-thread-safe const iterator over elements in a concurrent queue.

template<typename _Ty, class _Ax >
typedef const _Ty& Concurrency::concurrent_queue< _Ty, _Ax >::const_reference

A type that provides a reference to a const element stored in a concurrent queue for reading and performing const operations.

template<typename _Ty, class _Ax >
typedef std::ptrdiff_t Concurrency::concurrent_queue< _Ty, _Ax >::difference_type

A type that provides the signed distance between two elements in a concurrent queue.

template<typename _Ty, class _Ax >
typedef details::_Concurrent_queue_iterator<concurrent_queue,_Ty> Concurrency::concurrent_queue< _Ty, _Ax >::iterator

A type that represents a non-thread-safe iterator over the elements in a concurrent queue.

template<typename _Ty, class _Ax >
typedef _Ty& Concurrency::concurrent_queue< _Ty, _Ax >::reference

A type that provides a reference to an element stored in a concurrent queue.

template<typename _Ty, class _Ax >
typedef std::size_t Concurrency::concurrent_queue< _Ty, _Ax >::size_type

A type that counts the number of elements in a concurrent queue.

template<typename _Ty, class _Ax >
typedef _Ty Concurrency::concurrent_queue< _Ty, _Ax >::value_type

A type that represents the data type stored in a concurrent queue.

Constructor & Destructor Documentation

template<typename _Ty, class _Ax >
Concurrency::concurrent_queue< _Ty, _Ax >::concurrent_queue ( const allocator_type _Al = allocator_type())
inlineexplicit

Constructs a concurrent queue.

Parameters
_AlThe allocator class to use with this object.

All constructors store an allocator object _Al and initialize the queue.

The first constructor specifies an empty initial queue and explicitly specifies the allocator type to be used.

The second constructor specifies a copy of the concurrent queue _OtherQ .

The third constructor specifies a move of the concurrent queue _OtherQ .

The fourth constructor specifies values supplied by the iterator range [_Begin , _End ).

473  : _Concurrent_queue_base_v4( sizeof(_Ty) ), _My_allocator( _Al )
474  {
475  }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:356
_CONCRTIMP _Concurrent_queue_base_v4(size_t _Item_size)
template<typename _Ty , class _Ax >
Concurrency::concurrent_queue< _Ty, _Ax >::concurrent_queue ( const concurrent_queue< _Ty, _Ax > &  _Queue,
const allocator_type _Al = allocator_type() 
)

Constructs a concurrent queue.

Parameters
_OtherQThe source concurrent_queue object to copy or move elements from.
_AlThe allocator class to use with this object.

All constructors store an allocator object _Al and initialize the queue.

The first constructor specifies an empty initial queue and explicitly specifies the allocator type to be used.

The second constructor specifies a copy of the concurrent queue _OtherQ .

The third constructor specifies a move of the concurrent queue _OtherQ .

The fourth constructor specifies values supplied by the iterator range [_Begin , _End ).

768  : _Concurrent_queue_base_v4( sizeof(_Ty) ), _My_allocator(_Al)
769 {
770  concurrent_queue::const_iterator _QEnd = _Queue.unsafe_end();
771  for (concurrent_queue::const_iterator _It = _Queue.unsafe_begin(); _It != _QEnd; ++_It)
772  this->push(*_It);
773 }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:356
_CONCRTIMP _Concurrent_queue_base_v4(size_t _Item_size)
details::_Concurrent_queue_iterator< concurrent_queue, const _Ty > const_iterator
A type that represents a non-thread-safe const iterator over elements in a concurrent queue...
Definition: concurrent_queue.h:671
void push(const _Ty &_Src)
Enqueues an item at tail end of the concurrent queue. This method is concurrency-safe.
Definition: concurrent_queue.h:568
template<typename _Ty , class _Ax >
Concurrency::concurrent_queue< _Ty, _Ax >::concurrent_queue ( concurrent_queue< _Ty, _Ax > &&  _Queue,
const allocator_type _Al = allocator_type() 
)

Constructs a concurrent queue.

Parameters
_OtherQThe source concurrent_queue object to copy or move elements from.
_AlThe allocator class to use with this object.

All constructors store an allocator object _Al and initialize the queue.

The first constructor specifies an empty initial queue and explicitly specifies the allocator type to be used.

The second constructor specifies a copy of the concurrent queue _OtherQ .

The third constructor specifies a move of the concurrent queue _OtherQ .

The fourth constructor specifies values supplied by the iterator range [_Begin , _End ).

Parameters
_OtherQThe source concurrent_queue object to copy or move elements from.

All constructors store an allocator object _Al and initialize the queue.

The first constructor specifies an empty initial queue and explicitly specifies the allocator type to be used.

The second constructor specifies a copy of the concurrent queue _OtherQ .

The third constructor specifies a move of the concurrent queue _OtherQ .

The fourth constructor specifies values supplied by the iterator range [_Begin , _End ).

793  : _Concurrent_queue_base_v4( sizeof(_Ty) ), _My_allocator(_Al)
794 {
795  _Internal_swap(_Queue);
796 }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:356
_CONCRTIMP _Concurrent_queue_base_v4(size_t _Item_size)
_CONCRTIMP void _Internal_swap(_Concurrent_queue_base_v4 &other)
template<typename _Ty, class _Ax >
template<typename _InputIterator >
Concurrency::concurrent_queue< _Ty, _Ax >::concurrent_queue ( _InputIterator  _Begin,
_InputIterator  _End 
)
inline

Constructs a concurrent queue.

Template Parameters
_InputIteratorThe type of the input iterator that specifies a range of values.
Parameters
_BeginPosition of the first element in the range of elements to be copied.
_EndPosition of the first element beyond the range of elements to be copied.

All constructors store an allocator object _Al and initialize the queue.

The first constructor specifies an empty initial queue and explicitly specifies the allocator type to be used.

The second constructor specifies a copy of the concurrent queue _OtherQ .

The third constructor specifies a move of the concurrent queue _OtherQ .

The fourth constructor specifies values supplied by the iterator range [_Begin , _End ).

544  {
545  while (_Begin != _End)
546  {
547  this->push(*_Begin);
548  ++_Begin;
549  }
550  }
_Ax allocator_type
A type that represents the allocator class for the concurrent queue.
Definition: concurrent_queue.h:429
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:356
_CONCRTIMP _Concurrent_queue_base_v4(size_t _Item_size)
void push(const _Ty &_Src)
Enqueues an item at tail end of the concurrent queue. This method is concurrency-safe.
Definition: concurrent_queue.h:568
template<typename _Ty , class _Ax >
Concurrency::concurrent_queue< _Ty, _Ax >::~concurrent_queue ( )

Destroys the concurrent queue.

804 {
805  clear();
807 }
void clear()
Clears the concurrent queue, destroying any currently enqueued elements. This method is not concurren...
Definition: concurrent_queue.h:814

Member Function Documentation

template<typename _Ty, class _Ax >
virtual _Page* Concurrency::concurrent_queue< _Ty, _Ax >::_Allocate_page ( )
inlineprivatevirtual

Implements Concurrency::details::_Concurrent_queue_base_v4.

404  {
405  size_t _N = sizeof(_Page) + _Items_per_page*_Item_size;
406  _Page *_Pg = reinterpret_cast<_Page*>(_My_allocator.allocate( _N ));
407  if( !_Pg )
409  return _Pg;
410  }
size_t _Items_per_page
Definition: concurrent_queue.h:88
_CONCRTIMP void _Internal_throw_exception() const
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:356
size_t _Item_size
Definition: concurrent_queue.h:91
template<typename _Ty, class _Ax >
virtual void Concurrency::concurrent_queue< _Ty, _Ax >::_Assign_and_destroy_item ( void _Dst,
_Page _Src,
size_t  _Index 
)
inlineprivatevirtual

Implements Concurrency::details::_Concurrent_queue_base_v4.

394  {
395  _Ty& _From = _Get_ref(_Src,_Index);
396  _Destroyer _D(_From);
397  if (_Dst != NULL)
398  {
399  *static_cast<_Ty*>(_Dst) = std::move(_From);
400  }
401  }
#define NULL
Definition: vcruntime.h:236
_Ty & _Get_ref(_Page &_Pg, size_t _Index)
Definition: concurrent_queue.h:377
constexpr remove_reference< _Ty >::type && move(_Ty &&_Arg) _NOEXCEPT
Definition: type_traits:1290
template<typename _Ty, class _Ax >
virtual void Concurrency::concurrent_queue< _Ty, _Ax >::_Copy_item ( _Page _Dst,
size_t  _Index,
const void _Src 
)
inlineprivatevirtual

Implements Concurrency::details::_Concurrent_queue_base_v4.

384  {
385  new( &_Get_ref(_Dst,_Index) ) _Ty(*static_cast<const _Ty*>(_Src));
386  }
_Ty & _Get_ref(_Page &_Pg, size_t _Index)
Definition: concurrent_queue.h:377
template<typename _Ty, class _Ax >
virtual void Concurrency::concurrent_queue< _Ty, _Ax >::_Deallocate_page ( _Page _Pg)
inlineprivatevirtual

Implements Concurrency::details::_Concurrent_queue_base_v4.

413  {
414  size_t _N = sizeof(_Page) + _Items_per_page*_Item_size;
415  _My_allocator.deallocate( reinterpret_cast<char*>(_Pg), _N );
416  }
size_t _Items_per_page
Definition: concurrent_queue.h:88
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:356
size_t _Item_size
Definition: concurrent_queue.h:91
template<typename _Ty, class _Ax >
_Ty& Concurrency::concurrent_queue< _Ty, _Ax >::_Get_ref ( _Page _Pg,
size_t  _Index 
)
inlineprivate
378  {
380  return static_cast<_Ty*>(static_cast<void*>(&_Pg+1))[_Index];
381  }
size_t _Items_per_page
Definition: concurrent_queue.h:88
#define _CONCRT_ASSERT(x)
Definition: concrt.h:123
template<typename _Ty, class _Ax >
virtual void Concurrency::concurrent_queue< _Ty, _Ax >::_Move_item ( _Page _Dst,
size_t  _Index,
void _Src 
)
inlineprivatevirtual

Implements Concurrency::details::_Concurrent_queue_base_v4.

389  {
390  new( &_Get_ref(_Dst,_Index) ) _Ty(std::move(*static_cast<_Ty*>(_Src)));
391  }
_Ty & _Get_ref(_Page &_Pg, size_t _Index)
Definition: concurrent_queue.h:377
constexpr remove_reference< _Ty >::type && move(_Ty &&_Arg) _NOEXCEPT
Definition: type_traits:1290
template<typename _Ty , class _Ax >
void Concurrency::concurrent_queue< _Ty, _Ax >::clear ( )

Clears the concurrent queue, destroying any currently enqueued elements. This method is not concurrency-safe.

815 {
816  while( !empty() )
817  {
819  {
821  break;
822  }
823  }
824 }
#define NULL
Definition: vcruntime.h:236
#define _CONCRT_ASSERT(x)
Definition: concrt.h:123
bool empty() const
Tests if the concurrent queue is empty at the moment this method is called. This method is concurrenc...
Definition: concurrent_queue.h:638
_CONCRTIMP bool _Internal_pop_if_present(void *_Dst)
template<typename _Ty, class _Ax >
bool Concurrency::concurrent_queue< _Ty, _Ax >::empty ( ) const
inline

Tests if the concurrent queue is empty at the moment this method is called. This method is concurrency-safe.

Returns
true if the concurrent queue was empty at the moment we looked, false otherwise.

While this method is concurrency-safe with respect to calls to the methods push, try_pop, and empty, the value returned might be incorrect by the time it is inspected by the calling thread.

639  {
640  return _Internal_empty();
641  }
template<typename _Ty, class _Ax >
allocator_type Concurrency::concurrent_queue< _Ty, _Ax >::get_allocator ( ) const
inline

Returns a copy of the allocator used to construct the concurrent queue. This method is concurrency-safe.

Returns
A copy of the allocator used to construct the concurrent queue.
651  {
652  return this->_My_allocator;
653  }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:356
template<typename _Ty, class _Ax >
void Concurrency::concurrent_queue< _Ty, _Ax >::push ( const _Ty &  _Src)
inline

Enqueues an item at tail end of the concurrent queue. This method is concurrency-safe.

Parameters
_SrcThe item to be added to the queue.

push is concurrency-safe with respect to calls to the methods push, try_pop, and empty.

569  {
570  _Internal_push( &_Src );
571  }
_CONCRTIMP void _Internal_push(const void *_Src)
template<typename _Ty, class _Ax >
void Concurrency::concurrent_queue< _Ty, _Ax >::push ( _Ty &&  _Src)
inline

Enqueues an item at tail end of the concurrent queue. This method is concurrency-safe.

Parameters
_SrcThe item to be added to the queue.

push is concurrency-safe with respect to calls to the methods push, try_pop, and empty.

584  {
585  _Internal_move_push( &_Src );
586  }
_CONCRTIMP void _Internal_move_push(void *_Src)
template<typename _Ty, class _Ax >
bool Concurrency::concurrent_queue< _Ty, _Ax >::try_pop ( _Ty &  _Dest)
inline

Dequeues an item from the queue if one is available. This method is concurrency-safe.

Parameters
_DestA reference to a location to store the dequeued item.
Returns
true if an item was successfully dequeued,false otherwise.

If an item was successfully dequeued, the parameter _Dest receives the dequeued value, the original value held in the queue is destroyed, and this function returns true. If there was no item to dequeue, this function returns false without blocking, and the contents of the _Dest parameter are undefined.

try_pop is concurrency-safe with respect to calls to the methods push, try_pop, and empty.

607  {
608  return _Internal_pop_if_present( &_Dest );
609  }
_CONCRTIMP bool _Internal_pop_if_present(void *_Dst)
template<typename _Ty, class _Ax >
iterator Concurrency::concurrent_queue< _Ty, _Ax >::unsafe_begin ( )
inline

Returns an iterator of type iterator or const_iterator to the beginning of the concurrent queue. This method is not concurrency-safe.

Returns
An iterator of type iterator or const_iterator to the beginning of the concurrent queue object.

The iterators for the concurrent_queue class are primarily intended for debugging, as they are slow, and iteration is not concurrency-safe with respect to other queue operations.

687  {
688  return iterator(*this);
689  }
details::_Concurrent_queue_iterator< concurrent_queue, _Ty > iterator
A type that represents a non-thread-safe iterator over the elements in a concurrent queue...
Definition: concurrent_queue.h:665
template<typename _Ty, class _Ax >
const_iterator Concurrency::concurrent_queue< _Ty, _Ax >::unsafe_begin ( ) const
inline

Returns an iterator of type iterator or const_iterator to the beginning of the concurrent queue. This method is not concurrency-safe.

Returns
An iterator of type iterator or const_iterator to the beginning of the concurrent queue.

The iterators for the concurrent_queue class are primarily intended for debugging, as they are slow, and iteration is not concurrency-safe with respect to other queue operations.

723  {
724  return const_iterator(*this);
725  }
details::_Concurrent_queue_iterator< concurrent_queue, const _Ty > const_iterator
A type that represents a non-thread-safe const iterator over elements in a concurrent queue...
Definition: concurrent_queue.h:671
template<typename _Ty, class _Ax >
iterator Concurrency::concurrent_queue< _Ty, _Ax >::unsafe_end ( )
inline

Returns an iterator of type iterator or const_iterator to the end of the concurrent queue. This method is not concurrency-safe.

Returns
An iterator of type iterator or const_iterator to the end of the concurrent queue.

The iterators for the concurrent_queue class are primarily intended for debugging, as they are slow, and iteration is not concurrency-safe with respect to other queue operations.

705  {
706  return iterator();
707  }
details::_Concurrent_queue_iterator< concurrent_queue, _Ty > iterator
A type that represents a non-thread-safe iterator over the elements in a concurrent queue...
Definition: concurrent_queue.h:665
template<typename _Ty, class _Ax >
const_iterator Concurrency::concurrent_queue< _Ty, _Ax >::unsafe_end ( ) const
inline

Returns an iterator of type iterator or const_iterator to the end of the concurrent queue. This method is not concurrency-safe.

Returns
An iterator of type iterator or const_iterator to the end of the concurrent queue.

The iterators for the concurrent_queue class are primarily intended for debugging, as they are slow, and iteration is not concurrency-safe with respect to other queue operations.

741  {
742  return const_iterator();
743  }
details::_Concurrent_queue_iterator< concurrent_queue, const _Ty > const_iterator
A type that represents a non-thread-safe const iterator over elements in a concurrent queue...
Definition: concurrent_queue.h:671
template<typename _Ty, class _Ax >
size_type Concurrency::concurrent_queue< _Ty, _Ax >::unsafe_size ( ) const
inline

Returns the number of items in the queue. This method is not concurrency-safe.

Returns
The size of the concurrent queue.

unsafe_size is not concurrency-safe and can produce incorrect results if called concurrently with calls to the methods push, try_pop, and empty.

623  {
624  return _Internal_size();
625  }

Friends And Related Function Documentation

template<typename _Ty, class _Ax >
template<typename _Container , typename _Value >
friend class ::Concurrency::details::_Concurrent_queue_iterator
friend

Member Data Documentation

template<typename _Ty, class _Ax >
_Page_allocator_type Concurrency::concurrent_queue< _Ty, _Ax >::_My_allocator
private

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