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{};
83 mutable std::mutex m_mutex{};
84 std::condition_variable m_cv{};
85 volatile CPLWorkerThreadState eState = CPLWTS_OK;
89 CPLList *psWaitingWorkerThreadsList =
nullptr;
90 int nWaitingWorkerThreads = 0;
92 int m_nMaxThreads = 0;
94 static void WorkerThreadFunction(
void *user_data);
96 void DeclareJobFinished();
97 CPLWorkerThreadJob *GetNextJob(CPLWorkerThread *psWorkerThread);
103 bool Setup(
int nThreads, CPLThreadFunc pfnInitFunc,
void **pasInitData);
104 bool Setup(
int nThreads, CPLThreadFunc pfnInitFunc,
void **pasInitData,
105 bool bWaitallStarted);
107 std::unique_ptr<CPLJobQueue> CreateJobQueue();
109 bool SubmitJob(CPLThreadFunc pfnFunc,
void *pData);
110 bool SubmitJobs(CPLThreadFunc pfnFunc,
const std::vector<void *> &apData);
111 void WaitCompletion(
int nMaxRemainingJobs = 0);
115 int GetThreadCount()
const;
123 std::mutex m_mutex{};
124 std::condition_variable m_cv{};
125 int m_nPendingJobs = 0;
127 static void JobQueueFunction(
void *);
128 void DeclareJobFinished();
145 bool SubmitJob(CPLThreadFunc pfnFunc,
void *pData);
Job queue.
Definition: cpl_worker_thread_pool.h:120
CPLWorkerThreadPool * GetPool()
Return the owning worker thread pool.
Definition: cpl_worker_thread_pool.h:140
Pool of worker threads.
Definition: cpl_worker_thread_pool.h:79
bool SubmitJob(CPLThreadFunc pfnFunc, void *pData)
Queue a new job.
Definition: cpl_worker_thread_pool.cpp:141
void WaitCompletion(int nMaxRemainingJobs=0)
Wait for completion of part or whole jobs.
Definition: cpl_worker_thread_pool.cpp:412
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:1042
List element structure.
Definition: cpl_list.h:52