VTK  9.2.6
vtkThreadedTaskQueue.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkThreadedTaskQueue.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
53
54#ifndef vtkThreadedTaskQueue_h
55#define vtkThreadedTaskQueue_h
56
57#include "vtkObject.h"
58#include <atomic>
59#include <condition_variable>
60#include <cstdint>
61#include <functional>
62#include <memory>
63#include <mutex>
64#include <thread>
65
66#if !defined(__WRAP__)
68{
69template <typename R>
71
72template <typename R>
74};
75
76template <typename R, typename... Args>
78{
79public:
80 vtkThreadedTaskQueue(std::function<R(Args...)> worker, bool strict_ordering = true,
81 int buffer_size = -1, int max_concurrent_tasks = -1);
83
87 void Push(Args&&... args);
88
93 bool Pop(R& result);
94
99 bool TryPop(R& result);
100
105 bool IsEmpty() const;
106
110 void Flush();
111
112private:
114 void operator=(const vtkThreadedTaskQueue&) = delete;
115
116 std::function<R(Args...)> Worker;
117
118 std::unique_ptr<vtkThreadedTaskQueueInternals::TaskQueue<R>> Tasks;
119 std::unique_ptr<vtkThreadedTaskQueueInternals::ResultQueue<R>> Results;
120
121 int NumberOfThreads;
122 std::unique_ptr<std::thread[]> Threads;
123};
124
125template <typename... Args>
126class vtkThreadedTaskQueue<void, Args...>
127{
128public:
129 vtkThreadedTaskQueue(std::function<void(Args...)> worker, bool strict_ordering = true,
130 int buffer_size = -1, int max_concurrent_tasks = -1);
132
136 void Push(Args&&... args);
137
142 bool IsEmpty() const;
143
147 void Flush();
148
149private:
151 void operator=(const vtkThreadedTaskQueue&) = delete;
152
153 std::function<void(Args...)> Worker;
154
155 std::unique_ptr<vtkThreadedTaskQueueInternals::TaskQueue<void>> Tasks;
156
157 std::condition_variable ResultsCV;
158 std::mutex NextResultIdMutex;
159 std::atomic<std::uint64_t> NextResultId;
160
161 int NumberOfThreads;
162 std::unique_ptr<std::thread[]> Threads;
163};
164
165#include "vtkThreadedTaskQueue.txx"
166
167#endif // !defined(__WRAP__)
168
169#endif
170// VTK-HeaderTest-Exclude: vtkThreadedTaskQueue.h
bool Pop(R &result)
Pop the last result.
void Push(Args &&... args)
Push arguments for the work.
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.
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.
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)