30 #ifndef CPL_WORKER_THREAD_POOL_H_INCLUDED_
31 #define CPL_WORKER_THREAD_POOL_H_INCLUDED_
33 #include "cpl_multiproc.h"
36 #include <condition_variable>
49 struct CPLWorkerThreadJob;
52 struct CPLWorkerThread
55 CPLWorkerThread() =
default;
57 CPLThreadFunc pfnInitFunc =
nullptr;
58 void *pInitData =
nullptr;
60 CPLJoinableThread *hThread =
nullptr;
61 bool bMarkedAsWaiting =
false;
64 std::condition_variable m_cv{};
72 } CPLWorkerThreadState;
82 std::vector<std::unique_ptr<CPLWorkerThread>> aWT{};
84 std::condition_variable m_cv{};
85 volatile CPLWorkerThreadState eState = CPLWTS_OK;
89 CPLList* psWaitingWorkerThreadsList =
nullptr;
90 int nWaitingWorkerThreads = 0;
92 static void WorkerThreadFunction(
void* user_data);
94 void DeclareJobFinished();
95 CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread);
102 bool Setup(
int nThreads,
103 CPLThreadFunc pfnInitFunc,
105 bool Setup(
int nThreads,
106 CPLThreadFunc pfnInitFunc,
108 bool bWaitallStarted);
110 std::unique_ptr<CPLJobQueue> CreateJobQueue();
112 bool SubmitJob(CPLThreadFunc pfnFunc,
void* pData);
113 bool SubmitJobs(CPLThreadFunc pfnFunc,
const std::vector<void*>& apData);
114 void WaitCompletion(
int nMaxRemainingJobs = 0);
126 std::mutex m_mutex{};
127 std::condition_variable m_cv{};
128 int m_nPendingJobs = 0;
130 static void JobQueueFunction(
void*);
131 void DeclareJobFinished();
145 bool SubmitJob(CPLThreadFunc pfnFunc,
void* pData);
Job queue.
Definition: cpl_worker_thread_pool.h:123
CPLWorkerThreadPool * GetPool()
Return the owning worker thread pool.
Definition: cpl_worker_thread_pool.h:143
Pool of worker threads.
Definition: cpl_worker_thread_pool.h:79
int GetThreadCount() const
Return the number of threads setup.
Definition: cpl_worker_thread_pool.h:118
bool SubmitJob(CPLThreadFunc pfnFunc, void *pData)
Queue a new job.
Definition: cpl_worker_thread_pool.cpp:130
void WaitCompletion(int nMaxRemainingJobs=0)
Wait for completion of part or whole jobs.
Definition: cpl_worker_thread_pool.cpp:299
Simplest list implementation.
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition: cpl_port.h:927
List element structure.
Definition: cpl_list.h:52