VTK  9.4.20250202
vtkThreadedTaskQueue.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
42#ifndef vtkThreadedTaskQueue_h
43#define vtkThreadedTaskQueue_h
44
45#include "vtkObject.h"
46#include <atomic>
47#include <condition_variable>
48#include <cstdint>
49#include <functional>
50#include <memory>
51#include <mutex>
52#include <thread>
53
54#if !defined(__WRAP__)
56{
57VTK_ABI_NAMESPACE_BEGIN
58template <typename R>
60
61template <typename R>
63VTK_ABI_NAMESPACE_END
64}
65
66VTK_ABI_NAMESPACE_BEGIN
67
68template <typename R, typename... Args>
70{
71public:
72 vtkThreadedTaskQueue(std::function<R(Args...)> worker, bool strict_ordering = true,
73 int buffer_size = -1, int max_concurrent_tasks = -1);
75
79 void Push(Args&&... args);
80
85 bool Pop(R& result);
86
91 bool TryPop(R& result);
92
97 bool IsEmpty() const;
98
102 void Flush();
103
104private:
106 void operator=(const vtkThreadedTaskQueue&) = delete;
107
108 std::function<R(Args...)> Worker;
109
110 std::unique_ptr<vtkThreadedTaskQueueInternals::TaskQueue<R>> Tasks;
111 std::unique_ptr<vtkThreadedTaskQueueInternals::ResultQueue<R>> Results;
112
113 int NumberOfThreads;
114 std::unique_ptr<std::thread[]> Threads;
115};
116
117template <typename... Args>
118class vtkThreadedTaskQueue<void, Args...>
119{
120public:
121 vtkThreadedTaskQueue(std::function<void(Args...)> worker, bool strict_ordering = true,
122 int buffer_size = -1, int max_concurrent_tasks = -1);
124
128 void Push(Args&&... args);
129
134 bool IsEmpty() const;
135
139 void Flush();
140
141private:
143 void operator=(const vtkThreadedTaskQueue&) = delete;
144
145 std::function<void(Args...)> Worker;
146
147 std::unique_ptr<vtkThreadedTaskQueueInternals::TaskQueue<void>> Tasks;
148
149 std::condition_variable ResultsCV;
150 std::mutex NextResultIdMutex;
151 std::atomic<std::uint64_t> NextResultId;
152
153 int NumberOfThreads;
154 std::unique_ptr<std::thread[]> Threads;
155};
156
157VTK_ABI_NAMESPACE_END
158#include "vtkThreadedTaskQueue.txx"
159
160#endif // !defined(__WRAP__)
161
162#endif
163// VTK-HeaderTest-Exclude: vtkThreadedTaskQueue.h
void Push(Args &&... args)
Push arguments for the work.
void Flush()
Blocks till the queue becomes empty.
bool IsEmpty() const
Returns false if there's some result that may be popped right now or in the future.
vtkThreadedTaskQueue(std::function< void(Args...)> worker, bool strict_ordering=true, int buffer_size=-1, int max_concurrent_tasks=-1)
simple threaded task queue
bool Pop(R &result)
Pop the last result.
bool TryPop(R &result)
Attempt to pop without waiting.
vtkThreadedTaskQueue(std::function< R(Args...)> worker, bool strict_ordering=true, int buffer_size=-1, int max_concurrent_tasks=-1)
void Push(Args &&... args)
Push arguments for the work.
bool IsEmpty() const
Returns false if there's some result that may be popped right now or in the future.
void Flush()
Blocks till the queue becomes empty.