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
_CRTIMP2 _Concurrent_queue_base_v4 (size_t _Item_size)
 
virtual _CRTIMP2 ~_Concurrent_queue_base_v4 ()
 
_CRTIMP2 void _Internal_push (const void *_Src)
 
_CRTIMP2 void _Internal_move_push (void *_Src)
 
_CRTIMP2 void _Concurrent_queue_base_v4::_Internal_swap (_Concurrent_queue_base_v4 &other)
 
_CRTIMP2 bool _Internal_pop_if_present (void *_Dst)
 
_CRTIMP2 size_t _Internal_size () const
 
_CRTIMP2 bool _Internal_empty () const
 
_CRTIMP2 void _Internal_finish_clear ()
 
_CRTIMP2 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 ).

472  : _Concurrent_queue_base_v4( sizeof(_Ty) ), _My_allocator( _Al )
473  {
474  }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:355
_CRTIMP2 _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 ).

767  : _Concurrent_queue_base_v4( sizeof(_Ty) ), _My_allocator(_Al)
768 {
769  concurrent_queue::const_iterator _QEnd = _Queue.unsafe_end();
770  for (concurrent_queue::const_iterator _It = _Queue.unsafe_begin(); _It != _QEnd; ++_It)
771  this->push(*_It);
772 }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:355
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:670
_CRTIMP2 _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:567
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 ).

792  : _Concurrent_queue_base_v4( sizeof(_Ty) ), _My_allocator(_Al)
793 {
794  _Internal_swap(_Queue);
795 }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:355
_CRTIMP2 _Concurrent_queue_base_v4(size_t _Item_size)
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 ).

543  {
544  while (_Begin != _End)
545  {
546  this->push(*_Begin);
547  ++_Begin;
548  }
549  }
_Ax allocator_type
A type that represents the allocator class for the concurrent queue.
Definition: concurrent_queue.h:428
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:355
_CRTIMP2 _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:567
template<typename _Ty , class _Ax >
Concurrency::concurrent_queue< _Ty, _Ax >::~concurrent_queue ( )

Destroys the concurrent queue.

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

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.

403  {
404  size_t _N = sizeof(_Page) + _Items_per_page*_Item_size;
405  _Page *_Pg = reinterpret_cast<_Page*>(_My_allocator.allocate( _N ));
406  if( !_Pg )
408  return _Pg;
409  }
size_t _Items_per_page
Definition: concurrent_queue.h:87
_N
Definition: wchar.h:1269
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:355
size_t _Item_size
Definition: concurrent_queue.h:90
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.

393  {
394  _Ty& _From = _Get_ref(_Src,_Index);
395  _Destroyer _D(_From);
396  if (_Dst != NULL)
397  {
398  *static_cast<_Ty*>(_Dst) = std::move(_From);
399  }
400  }
_Ty & _Get_ref(_Page &_Pg, size_t _Index)
Definition: concurrent_queue.h:376
_OutIt move(_InIt _First, _InIt _Last, _OutIt _Dest)
Definition: xutility:2447
#define NULL
Definition: crtdbg.h:30
_In_ size_t _In_z_ const unsigned char * _Src
Definition: mbstring.h:95
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.

383  {
384  new( &_Get_ref(_Dst,_Index) ) _Ty(*static_cast<const _Ty*>(_Src));
385  }
_Ty & _Get_ref(_Page &_Pg, size_t _Index)
Definition: concurrent_queue.h:376
_In_ size_t _In_z_ const unsigned char * _Src
Definition: mbstring.h:95
template<typename _Ty, class _Ax >
virtual void Concurrency::concurrent_queue< _Ty, _Ax >::_Deallocate_page ( _Page _Pg)
inlineprivatevirtual

Implements Concurrency::details::_Concurrent_queue_base_v4.

412  {
413  size_t _N = sizeof(_Page) + _Items_per_page*_Item_size;
414  _My_allocator.deallocate( reinterpret_cast<char*>(_Pg), _N );
415  }
size_t _Items_per_page
Definition: concurrent_queue.h:87
_N
Definition: wchar.h:1269
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:355
size_t _Item_size
Definition: concurrent_queue.h:90
template<typename _Ty, class _Ax >
_Ty& Concurrency::concurrent_queue< _Ty, _Ax >::_Get_ref ( _Page _Pg,
size_t  _Index 
)
inlineprivate
377  {
379  return static_cast<_Ty*>(static_cast<void*>(&_Pg+1))[_Index];
380  }
size_t _Items_per_page
Definition: concurrent_queue.h:87
#define _CONCRT_ASSERT(x)
Definition: concrt.h:137
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.

388  {
389  new( &_Get_ref(_Dst,_Index) ) _Ty(std::move(*static_cast<_Ty*>(_Src)));
390  }
_Ty & _Get_ref(_Page &_Pg, size_t _Index)
Definition: concurrent_queue.h:376
_OutIt move(_InIt _First, _InIt _Last, _OutIt _Dest)
Definition: xutility:2447
_In_ size_t _In_z_ const unsigned char * _Src
Definition: mbstring.h:95
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.

814 {
815  while( !empty() )
816  {
818  {
820  break;
821  }
822  }
823 }
#define _CONCRT_ASSERT(x)
Definition: concrt.h:137
#define NULL
Definition: crtdbg.h:30
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:637
_CRTIMP2 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.

638  {
639  return _Internal_empty();
640  }
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.
650  {
651  return this->_My_allocator;
652  }
_Page_allocator_type _My_allocator
Definition: concurrent_queue.h:355
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.

568  {
569  _Internal_push( &_Src );
570  }
_CRTIMP2 void _Internal_push(const void *_Src)
_In_ size_t _In_z_ const unsigned char * _Src
Definition: mbstring.h:95
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.

583  {
585  }
_In_ size_t _In_z_ const unsigned char * _Src
Definition: mbstring.h:95
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.

606  {
607  return _Internal_pop_if_present( &_Dest );
608  }
_CRTIMP2 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.

686  {
687  return iterator(*this);
688  }
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:664
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.

722  {
723  return const_iterator(*this);
724  }
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:670
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.

704  {
705  return iterator();
706  }
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:664
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.

740  {
741  return const_iterator();
742  }
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:670
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.

622  {
623  return _Internal_size();
624  }

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: