VTK: Parallel/Core/vtkThreadedTaskQueue.h Source File

| VTK 9.3.1

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__)
55 namespace vtkThreadedTaskQueueInternals
56 {
57 VTK_ABI_NAMESPACE_BEGIN
58 template <typename R>
59 class TaskQueue;
60
61 template <typename R>
62 class ResultQueue;
63 VTK_ABI_NAMESPACE_END
64 }
65
66 VTK_ABI_NAMESPACE_BEGIN
67
68 template <typename R, typename... Args>
69 class vtkThreadedTaskQueue
70 {
71 public:
72 vtkThreadedTaskQueue(std::function<R(Args...)> worker, bool strict_ordering = true,
73 int buffer_size = -1, int max_concurrent_tasks = -1);
74 ~vtkThreadedTaskQueue();
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
104 private:
105 vtkThreadedTaskQueue(const vtkThreadedTaskQueue&) = delete;
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
117 template <typename... Args>
118 class vtkThreadedTaskQueue<void, Args...>
119 {
120 public:
121 vtkThreadedTaskQueue(std::function<void(Args...)> worker, bool strict_ordering = true,
122 int buffer_size = -1, int max_concurrent_tasks = -1);
123 ~vtkThreadedTaskQueue();
124
128 void Push(Args&&... args);
129
134 bool IsEmpty() const;
135
139 void Flush();
140
141 private:
142 vtkThreadedTaskQueue(const vtkThreadedTaskQueue&) = delete;
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
157 VTK_ABI_NAMESPACE_END
158 #include "vtkThreadedTaskQueue.txx"
159
160 #endif // !defined(__WRAP__)
161
162 #endif
163 // VTK-HeaderTest-Exclude: vtkThreadedTaskQueue.h
vtkThreadedTaskQueueInternals::ResultQueue
Definition: vtkThreadedTaskQueue.h:62
vtkThreadedTaskQueueInternals::TaskQueue
Definition: vtkThreadedTaskQueue.h:59
vtkThreadedTaskQueue< void, Args... >::Push
void Push(Args &&... args)
Push arguments for the work.
vtkThreadedTaskQueue< void, Args... >::Flush
void Flush()
Blocks till the queue becomes empty.
vtkThreadedTaskQueue< void, Args... >::IsEmpty
bool IsEmpty() const
Returns false if there's some result that may be popped right now or in the future.
vtkThreadedTaskQueue< void, Args... >::vtkThreadedTaskQueue
vtkThreadedTaskQueue(std::function< void(Args...)> worker, bool strict_ordering=true, int buffer_size=-1, int max_concurrent_tasks=-1)
vtkThreadedTaskQueue< void, Args... >::~vtkThreadedTaskQueue
~vtkThreadedTaskQueue()
vtkThreadedTaskQueue
simple threaded task queue
Definition: vtkThreadedTaskQueue.h:70
vtkThreadedTaskQueue::Pop
bool Pop(R &result)
Pop the last result.
vtkThreadedTaskQueue::TryPop
bool TryPop(R &result)
Attempt to pop without waiting.
vtkThreadedTaskQueue::vtkThreadedTaskQueue
vtkThreadedTaskQueue(std::function< R(Args...)> worker, bool strict_ordering=true, int buffer_size=-1, int max_concurrent_tasks=-1)
vtkThreadedTaskQueue::Push
void Push(Args &&... args)
Push arguments for the work.
vtkThreadedTaskQueue::IsEmpty
bool IsEmpty() const
Returns false if there's some result that may be popped right now or in the future.
vtkThreadedTaskQueue::Flush
void Flush()
Blocks till the queue becomes empty.
vtkThreadedTaskQueue::~vtkThreadedTaskQueue
~vtkThreadedTaskQueue()
vtkThreadedTaskQueueInternals
Definition: vtkThreadedTaskQueue.h:56
vtkX3D::function
@ function
Definition: vtkX3D.h:249
vtkObject.h