DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ConcurrentBag.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 ConcurrentBag : public Object {
23 private:
24 mutable Threading::CriticalSection m_csLock;
26
27 public:
29 ConcurrentBag() = default;
30
32 ~ConcurrentBag() override = default;
33
36 void Add(const T& item) {
37 Threading::CriticalSectionLock lock(m_csLock);
38 m_list.AddFirst(item);
39 }
40
44 bool TryTake(T& result) {
45 Threading::CriticalSectionLock lock(m_csLock);
46 if (m_list.GetCount() == 0) return false;
47
48 result = m_list.GetFirst()->Value;
49 m_list.RemoveFirst();
50 return true;
51 }
52
56 bool TryPeek(T& result) const {
57 Threading::CriticalSectionLock lock(m_csLock);
58 if (m_list.GetCount() == 0) return false;
59
60 result = m_list.GetFirst()->Value;
61 return true;
62 }
63
64 void Clear() {
65 Threading::CriticalSectionLock lock(m_csLock);
66 m_list.Clear();
67 }
68
69 int GetCount() const {
70 Threading::CriticalSectionLock lock(m_csLock);
71 return m_list.GetCount();
72 }
73
74 bool IsEmpty() const {
75 Threading::CriticalSectionLock lock(m_csLock);
76 return m_list.GetCount() == 0;
77 }
78
79 Array<T> ToArray() const {
80 Threading::CriticalSectionLock lock(m_csLock);
81 return m_list.ToArray();
82 }
83 };
84
85 }
86 }
87 }
88}
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 ConcurrentBag without removing it.
void Add(const T &item)
Adds an object to the ConcurrentBag.
bool TryTake(T &result)
Attempts to remove and return an object from the ConcurrentBag.
ConcurrentBag()=default
Initializes a new instance of the ConcurrentBag class that is empty.
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