AzeorthCore 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 ()
 
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
37: _shutdown(false) { }
std::atomic< bool > _shutdown
Definition: PCQueue.h:34

Member Function Documentation

◆ Cancel()

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

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
116{ 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
119{ }

◆ Empty()

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

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

◆ Pop()

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

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

◆ Push()

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

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 >
size_t ProducerConsumerQueue< T >::Size ( ) const
inline
55 {
56 return _queue.size();
57 }

References ProducerConsumerQueue< T >::_queue.

◆ WaitAndPop()

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

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