Sleeping Wombat Common Library  0.50.0
swCommonLibrary
QueueMT.h
Go to the documentation of this file.
1 #pragma once
2 
9 #include <queue>
10 #include <mutex>
11 #include <condition_variable>
12 
13 
14 namespace sw
15 {
16 
17 
18 
20 template< typename ContentType >
21 class QueueMT
22 {
23 private:
24 
25  std::queue< ContentType > m_queue;
26  std::mutex m_accesslock;
27  std::condition_variable m_condition;
28 
29 public:
30 
31  void Push ( const ContentType& element );
32  void Push ( ContentType&& element );
33  bool TryPop ( ContentType& element );
34  void Pop ( ContentType& element );
35 
36  bool IsEmpty ();
37 
38 };
39 
41 template< typename ContentType >
42 inline void QueueMT< ContentType >::Push ( const ContentType& element )
43 {
44  std::lock_guard< std::mutex > guard( m_accesslock );
45  m_queue.push( element );
46 }
47 
49 template< typename ContentType >
50 inline void QueueMT< ContentType >::Push ( ContentType&& element )
51 {
52  std::lock_guard< std::mutex > guard( m_accesslock );
53  m_queue.push( std::move( element ) );
54 }
55 
56 
58 template< typename ContentType >
59 inline void QueueMT< ContentType >::Pop ( ContentType & element )
60 {
61  std::unique_lock< std::mutex > guard( m_accesslock );
62 
63  while( m_queue.empty() )
64  {
65  m_condition.wait( guard );
66  }
67 
68  element = std::move( m_queue.front() );
69  m_queue.pop();
70 }
71 
73 template< typename ContentType >
74 inline bool QueueMT< ContentType >::TryPop ( ContentType & element )
75 {
76  std::unique_lock< std::mutex > guard( m_accesslock );
77 
78  if( m_queue.empty() )
79  {
80  return false;
81  }
82 
83  element = std::move( m_queue.front() );
84  m_queue.pop();
85  return true;
86 }
87 
91 template< typename ContentType >
93 {
94  std::lock_guard< std::mutex > guard( m_accesslock );
95  return m_queue.empty();
96 }
97 
98 
99 
100 } // sw
101 
bool IsEmpty()
Zwraca true, jeżeli kolejka jest pusta. Należy pamiętać, że po wywołaniu tej funkcji kolejka może zos...
Definition: QueueMT.h:92
Definition: Exception.h:11
void Pop(ContentType &element)
Returns element in parameter.
Definition: QueueMT.h:59
Concurrent queue.
Definition: QueueMT.h:21