13#ifndef CPL_WORKER_THREAD_POOL_H_INCLUDED_
14#define CPL_WORKER_THREAD_POOL_H_INCLUDED_
16#include "cpl_multiproc.h"
19#include <condition_variable>
38 CPLWorkerThread() =
default;
40 CPLThreadFunc pfnInitFunc =
nullptr;
41 void *pInitData =
nullptr;
43 CPLJoinableThread *hThread =
nullptr;
44 bool bMarkedAsWaiting =
false;
47 std::condition_variable m_cv{};
55} CPLWorkerThreadState;
67 std::vector<std::unique_ptr<CPLWorkerThread>> aWT{};
68 mutable std::mutex m_mutex{};
69 std::condition_variable m_cv{};
70 volatile CPLWorkerThreadState eState = CPLWTS_OK;
71 std::queue<std::function<void()>> jobQueue;
73 bool m_bNotifyEvent =
false;
75 CPLList *psWaitingWorkerThreadsList =
nullptr;
76 int nWaitingWorkerThreads = 0;
78 int m_nMaxThreads = 0;
80 static void WorkerThreadFunction(
void *user_data);
82 void DeclareJobFinished();
83 std::function<void()> GetNextJob(CPLWorkerThread *psWorkerThread);
90 bool Setup(
int nThreads, CPLThreadFunc pfnInitFunc,
void **pasInitData);
91 bool Setup(
int nThreads, CPLThreadFunc pfnInitFunc,
void **pasInitData,
92 bool bWaitallStarted);
96 bool SubmitJob(std::function<
void()> task);
97 bool SubmitJob(CPLThreadFunc pfnFunc,
void *pData);
98 bool SubmitJobs(CPLThreadFunc pfnFunc,
const std::vector<void *> &apData);
99 void WaitCompletion(
int nMaxRemainingJobs = 0);
101 void WakeUpWaitEvent();
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);
135 void WaitCompletion(
int nMaxRemainingJobs = 0);
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:64
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:1101
std::unique_ptr< CPLJobQueue > CPLJobQueuePtr
Unique pointer to a job queue.
Definition cpl_worker_thread_pool.h:60
List element structure.
Definition cpl_list.h:35