14#ifndef CPL_WORKER_THREAD_POOL_H_INCLUDED_
15#define CPL_WORKER_THREAD_POOL_H_INCLUDED_
17#include "cpl_multiproc.h"
20#include <condition_variable>
40 CPLWorkerThread() =
default;
42 CPLThreadFunc pfnInitFunc =
nullptr;
43 void *pInitData =
nullptr;
45 CPLJoinableThread *hThread =
nullptr;
46 bool bMarkedAsWaiting =
false;
49 std::condition_variable m_cv{};
57} CPLWorkerThreadState;
69 std::vector<std::unique_ptr<CPLWorkerThread>> aWT{};
70 mutable std::mutex m_mutex{};
71 std::condition_variable m_cv{};
72 volatile CPLWorkerThreadState eState = CPLWTS_OK;
73 std::queue<std::function<void()>> jobQueue;
76 CPLList *psWaitingWorkerThreadsList =
nullptr;
77 int nWaitingWorkerThreads = 0;
79 int m_nMaxThreads = 0;
81 static void WorkerThreadFunction(
void *user_data);
83 void DeclareJobFinished();
84 std::function<void()> GetNextJob(CPLWorkerThread *psWorkerThread);
91 bool Setup(
int nThreads, CPLThreadFunc pfnInitFunc,
void **pasInitData);
92 bool Setup(
int nThreads, CPLThreadFunc pfnInitFunc,
void **pasInitData,
93 bool bWaitallStarted);
97 bool SubmitJob(std::function<
void()> task);
98 bool SubmitJob(CPLThreadFunc pfnFunc,
void *pData);
99 bool SubmitJobs(CPLThreadFunc pfnFunc,
const std::vector<void *> &apData);
100 void WaitCompletion(
int nMaxRemainingJobs = 0);
104 int GetThreadCount()
const;
112 std::mutex m_mutex{};
113 std::condition_variable m_cv{};
114 int m_nPendingJobs = 0;
116 void DeclareJobFinished();
133 bool SubmitJob(CPLThreadFunc pfnFunc,
void *pData);
134 bool SubmitJob(std::function<
void()> task);
Job queue.
Definition: cpl_worker_thread_pool.h:109
CPLWorkerThreadPool * GetPool()
Return the owning worker thread pool.
Definition: cpl_worker_thread_pool.h:128
Pool of worker threads.
Definition: cpl_worker_thread_pool.h:66
void WaitEvent()
Wait for completion of at least one job, if there are any remaining.
Definition: cpl_worker_thread_pool.cpp:345
bool SubmitJob(std::function< void()> task)
Queue a new job.
Definition: cpl_worker_thread_pool.cpp:134
void WaitCompletion(int nMaxRemainingJobs=0)
Wait for completion of part or whole jobs.
Definition: cpl_worker_thread_pool.cpp:330
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:1030
std::unique_ptr< CPLJobQueue > CPLJobQueuePtr
Unique pointer to a job queue.
Definition: cpl_worker_thread_pool.h:62
List element structure.
Definition: cpl_list.h:36