AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
ProducerConsumerQueue< T > Class Template Reference

#include "PCQueue.h"

Public Member Functions

 ProducerConsumerQueue ()
 
void Push (const T &value)
 
bool Empty ()
 
std::size_t Size () const
 
bool Pop (T &value)
 
void WaitAndPop (T &value)
 
void Cancel ()
 

Private Member Functions

template<typename E = T>
std::enable_if< std::is_pointer< E >::value >::type DeleteQueuedObject (E &obj)
 
template<typename E = T>
std::enable_if<!std::is_pointer< E >::value >::type DeleteQueuedObject (E const &)
 

Private Attributes

std::mutex _queueLock
 
std::queue< T > _queue
 
std::condition_variable _condition
 
std::atomic< bool > _shutdown
 

Detailed Description

template<typename T>
class ProducerConsumerQueue< T >

Constructor & Destructor Documentation

◆ ProducerConsumerQueue()

template<typename T >
ProducerConsumerQueue< T >::ProducerConsumerQueue ( )
inline
34: _shutdown(false) { }
std::atomic< bool > _shutdown
Definition: PCQueue.h:31

Member Function Documentation

◆ Cancel()

template<typename T >
void ProducerConsumerQueue< T >::Cancel ( )
inline
94 {
95 std::unique_lock<std::mutex> lock(_queueLock);
96
97 while (!_queue.empty())
98 {
99 T& value = _queue.front();
100
101 DeleteQueuedObject(value);
102
103 _queue.pop();
104 }
105
106 _shutdown = true;
107
108 _condition.notify_all();
109 }
std::mutex _queueLock
Definition: PCQueue.h:28
std::queue< T > _queue
Definition: PCQueue.h:29
std::condition_variable _condition
Definition: PCQueue.h:30
std::enable_if< std::is_pointer< E >::value >::type DeleteQueuedObject(E &obj)
Definition: PCQueue.h:113

References ProducerConsumerQueue< T >::_condition, ProducerConsumerQueue< T >::_queue, ProducerConsumerQueue< T >::_queueLock, ProducerConsumerQueue< T >::_shutdown, and ProducerConsumerQueue< T >::DeleteQueuedObject().

Referenced by MapUpdater::deactivate(), and DatabaseWorker::~DatabaseWorker().

◆ DeleteQueuedObject() [1/2]

template<typename T >
template<typename E = T>
std::enable_if< std::is_pointer< E >::value >::type ProducerConsumerQueue< T >::DeleteQueuedObject ( E &  obj)
inlineprivate
113{ delete obj; }

Referenced by ProducerConsumerQueue< T >::Cancel().

◆ DeleteQueuedObject() [2/2]

template<typename T >
template<typename E = T>
std::enable_if<!std::is_pointer< E >::value >::type ProducerConsumerQueue< T >::DeleteQueuedObject ( E const &  )
inlineprivate
116{ }

◆ Empty()

template<typename T >
bool ProducerConsumerQueue< T >::Empty ( )
inline
45 {
46 std::lock_guard<std::mutex> lock(_queueLock);
47
48 return _queue.empty();
49 }

References ProducerConsumerQueue< T >::_queue, and ProducerConsumerQueue< T >::_queueLock.

◆ Pop()

template<typename T >
bool ProducerConsumerQueue< T >::Pop ( T &  value)
inline
57 {
58 std::lock_guard<std::mutex> lock(_queueLock);
59
60 if (_queue.empty() || _shutdown)
61 {
62 return false;
63 }
64
65 value = _queue.front();
66
67 _queue.pop();
68
69 return true;
70 }

References ProducerConsumerQueue< T >::_queue, ProducerConsumerQueue< T >::_queueLock, and ProducerConsumerQueue< T >::_shutdown.

◆ Push()

template<typename T >
void ProducerConsumerQueue< T >::Push ( const T &  value)
inline
37 {
38 std::lock_guard<std::mutex> lock(_queueLock);
39 _queue.push(std::move(value));
40
41 _condition.notify_one();
42 }

References ProducerConsumerQueue< T >::_condition, ProducerConsumerQueue< T >::_queue, and ProducerConsumerQueue< T >::_queueLock.

Referenced by MapUpdater::schedule_lfg_update(), and MapUpdater::schedule_update().

◆ Size()

template<typename T >
std::size_t ProducerConsumerQueue< T >::Size ( ) const
inline
52 {
53 return _queue.size();
54 }

References ProducerConsumerQueue< T >::_queue.

◆ WaitAndPop()

template<typename T >
void ProducerConsumerQueue< T >::WaitAndPop ( T &  value)
inline
73 {
74 std::unique_lock<std::mutex> lock(_queueLock);
75
76 // we could be using .wait(lock, predicate) overload here but it is broken
77 // https://connect.microsoft.com/VisualStudio/feedback/details/1098841
78 while (_queue.empty() && !_shutdown)
79 {
80 _condition.wait(lock);
81 }
82
83 if (_queue.empty() || _shutdown)
84 {
85 return;
86 }
87
88 value = _queue.front();
89
90 _queue.pop();
91 }

References ProducerConsumerQueue< T >::_condition, ProducerConsumerQueue< T >::_queue, ProducerConsumerQueue< T >::_queueLock, and ProducerConsumerQueue< T >::_shutdown.

Referenced by DatabaseWorker::WorkerThread(), and MapUpdater::WorkerThread().

Member Data Documentation

◆ _condition

template<typename T >
std::condition_variable ProducerConsumerQueue< T >::_condition
private

◆ _queue

◆ _queueLock

◆ _shutdown

template<typename T >
std::atomic<bool> ProducerConsumerQueue< T >::_shutdown
private