17 struct ThreadPoolTask {
25 class ThreadPoolInternal {
27 static ThreadPoolInternal& GetInstance() {
29 static ThreadPoolInternal instance;
36 Lock<CriticalSection> lock(m_csSync);
37 if (m_bIsShuttingDown)
return false;
39 ThreadPoolTask objTask;
40 objTask.Callback = callback;
41 objTask.State = pState;
42 m_qTasks.Add(objTask);
45 m_evtWorkAvailable.Set();
49 bool SetMinThreads(
int iMinThreads) {
51 if (iMinThreads <= 0)
return false;
52 Lock<CriticalSection> lock(m_csSync);
53 if (m_bIsShuttingDown)
return false;
55 while (m_pvWorkerThreads.GetCount() < iMinThreads) {
58 m_pvWorkerThreads.Add(std::move(pWorker));
65 : m_bIsShuttingDown(false), m_evtWorkAvailable(false, false) {
68 if (iThreadCount < 10) iThreadCount = 10;
70 for (
int i = 0; i < iThreadCount; ++i) {
73 m_pvWorkerThreads.Add(std::move(pWorker));
77 ~ThreadPoolInternal() {
80 Lock<CriticalSection> lock(m_csSync);
81 m_bIsShuttingDown =
true;
85 for (
int i = 0; i < m_pvWorkerThreads.GetCount(); ++i) {
86 m_evtWorkAvailable.Set();
89 for (
int i = 0; i < m_pvWorkerThreads.GetCount(); ++i) {
90 SmartPointer<Thread> pWorker = m_pvWorkerThreads[i];
91 if (!pWorker.IsNull()) {
100 ThreadPoolTask objTask;
101 bool bHasTask =
false;
104 Lock<CriticalSection> lock(m_csSync);
105 if (m_qTasks.GetCount() > 0) {
106 objTask = m_qTasks[0];
107 m_qTasks.RemoveAt(0);
111 if (m_qTasks.GetCount() > 0) {
112 m_evtWorkAvailable.Set();
114 }
else if (m_bIsShuttingDown) {
116 m_evtWorkAvailable.Set();
122 if (objTask.Callback) {
124 objTask.Callback(objTask.State);
125 }
catch (
const Exception&) {
127 }
catch (
const std::exception& ex) {
130 (void)
UnknownException(
"An unhandled exception occurred during ThreadPool task execution.");
135 m_evtWorkAvailable.WaitOne();
140 Collections::Generic::List<SmartPointer<Thread>> m_pvWorkerThreads;
141 Collections::Generic::List<ThreadPoolTask> m_qTasks;
142 CriticalSection m_csSync;
143 EventWaitHandle m_evtWorkAvailable;
144 bool m_bIsShuttingDown;
154 return ThreadPoolInternal::GetInstance().QueueTask(callback, pState);
159 return ThreadPoolInternal::GetInstance().SetMinThreads(iMinThreads);
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Provides information about, and means to manipulate, the current environment and platform.
Represents a thread synchronization event supporting automatic and manual reset modes.
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Provides an RAII-style scoped lock wrapper around synchronization primitives.
Creates and controls a thread, sets its priority, and gets its status mirroring .NET System....
Provides a pool of threads that can be used to execute tasks and work items.
Represents an unknown or unmapped exception encountered during execution.
static int GetProcessorCount()
Gets the number of logical processors available on the current machine.
Supports all classes in the DotNetDupe class hierarchy.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
static bool SetMinThreads(int iMinThreads)
Sets the minimum number of threads the thread pool creates on demand as new requests are made.
static bool QueueUserWorkItem(WaitCallback callback)
Queues a method for execution. The method executes when a thread pool thread becomes available.
UnknownException()
Initializes a new instance of the UnknownException class with a default message.
Action< Object * > WaitCallback
Represents a callback method to be executed by a thread pool thread.