DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ConcurrentQueue.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "Common.h"
7#include "System/Object.h"
8#include "System/Array.h"
12
13namespace DotNetDupe {
14 namespace System {
15 namespace Collections {
16 namespace Concurrent {
17
24 template <typename T>
25 class ConcurrentQueue : public Object {
26 private:
27 mutable Threading::CriticalSection m_csLock;
29
30 public:
32 ConcurrentQueue() = default;
33
36 void Enqueue(const T& item) {
38 Threading::CriticalSectionLock lock(m_csLock);
40 m_list.AddLast(item);
41 }
42
46 bool TryDequeue(T& result) {
48 Threading::CriticalSectionLock lock(m_csLock);
49 if (m_list.GetCount() == 0) {
50 return false;
51 }
52
54 result = m_list.GetFirst()->Value;
55 m_list.RemoveFirst();
56 return true;
57 }
58
62 bool TryPeek(T& result) const {
64 Threading::CriticalSectionLock lock(m_csLock);
65 if (m_list.GetCount() == 0) {
66 return false;
67 }
68
70 result = m_list.GetFirst()->Value;
71 return true;
72 }
73
75 void Clear() {
77 Threading::CriticalSectionLock lock(m_csLock);
79 m_list.Clear();
80 }
81
84 int GetCount() const {
86 Threading::CriticalSectionLock lock(m_csLock);
88 return m_list.GetCount();
89 }
90
93 bool IsEmpty() const {
95 Threading::CriticalSectionLock lock(m_csLock);
97 return m_list.GetCount() == 0;
98 }
99
104 Threading::CriticalSectionLock lock(m_csLock);
106 return m_list.ToArray();
107 }
108 };
109
110 }
111 }
112 }
113}
Provides methods for creating, manipulating, searching, and sorting arrays.
Defines common cross-platform macros, export decorators, and fundamental types.
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Provides an RAII-style scoped lock wrapper around synchronization primitives.
Base object class for DotNetDupe mirroring .NET System.Object.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
Array< T > ToArray() const
Copies the elements stored in the ConcurrentQueue to a new array.
int GetCount() const
Gets the number of elements contained in the ConcurrentQueue.
ConcurrentQueue()=default
Initializes a new instance of the ConcurrentQueue class.
void Enqueue(const T &item)
Adds an object to the end of the ConcurrentQueue.
bool TryPeek(T &result) const
Attempts to return an object from the beginning of the queue without removing it.
bool IsEmpty() const
Gets a value that indicates whether the ConcurrentQueue is empty.
bool TryDequeue(T &result)
Attempts to remove and return the object at the beginning of the queue.
void Clear()
Removes all objects from the ConcurrentQueue.
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Lock< CriticalSection > CriticalSectionLock
Convenience alias for Lock<CriticalSection>.
Definition Lock.h:71