DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ConcurrentStack.h
Go to the documentation of this file.
1#pragma once
2
3#include "Common.h"
4#include "System/Object.h"
5#include "System/Array.h"
9
10namespace DotNetDupe {
11 namespace System {
12 namespace Collections {
13 namespace Concurrent {
14
21 template <typename T>
22 class ConcurrentStack : public Object {
23 private:
24 mutable Threading::CriticalSection m_csLock;
26
27 public:
29 ConcurrentStack() = default;
30
33 void Push(const T& item) {
34 Threading::CriticalSectionLock lock(m_csLock);
35 m_list.AddFirst(item);
36 }
37
41 bool TryPop(T& result) {
42 Threading::CriticalSectionLock lock(m_csLock);
43 if (m_list.GetCount() == 0) {
44 return false;
45 }
46
47 result = m_list.GetFirst()->Value;
48 m_list.RemoveFirst();
49 return true;
50 }
51
55 bool TryPeek(T& result) const {
56 Threading::CriticalSectionLock lock(m_csLock);
57 if (m_list.GetCount() == 0) {
58 return false;
59 }
60
61 result = m_list.GetFirst()->Value;
62 return true;
63 }
64
65 void Clear() {
66 Threading::CriticalSectionLock lock(m_csLock);
67 m_list.Clear();
68 }
69
70 int GetCount() const {
71 Threading::CriticalSectionLock lock(m_csLock);
72 return m_list.GetCount();
73 }
74
75 bool IsEmpty() const {
76 Threading::CriticalSectionLock lock(m_csLock);
77 return m_list.GetCount() == 0;
78 }
79
80 Array<T> ToArray() const {
81 Threading::CriticalSectionLock lock(m_csLock);
82 return m_list.ToArray();
83 }
84 };
85
86 }
87 }
88 }
89}
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
bool TryPeek(T &result) const
Attempts to return an object from the top of the ConcurrentStack without removing it.
bool TryPop(T &result)
Attempts to pop and return the object at the top of the ConcurrentStack.
ConcurrentStack()=default
Initializes a new instance of the ConcurrentStack class that is empty.
void Push(const T &item)
Inserts an object at the top of the ConcurrentStack.
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