VTK  9.1.0
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=========================================================================*/
51#ifndef vtkThreadedTaskQueue_h
52#define vtkThreadedTaskQueue_h
53
54#include "vtkObject.h"
55#include <atomic>
56#include <condition_variable>
57#include <cstdint>
58#include <functional>
59#include <memory>
60#include <mutex>
61#include <thread>
62
63#if !defined(__WRAP__)
65{
66template <typename R>
68
69template <typename R>
71};
72
73template <typename R, typename... Args>
75{
76public:
77 vtkThreadedTaskQueue(std::function<R(Args...)> worker, bool strict_ordering = true,
78 int buffer_size = -1, int max_concurrent_tasks = -1);
80
84 void Push(Args&&... args);
85
90 bool Pop(R& result);
91
96 bool TryPop(R& result);
97
102 bool IsEmpty() const;
103
107 void Flush();
108
109private:
111 void operator=(const vtkThreadedTaskQueue&) = delete;
112
113 std::function<R(Args...)> Worker;
114
115 std::unique_ptr<vtkThreadedTaskQueueInternals::TaskQueue<R>> Tasks;
116 std::unique_ptr<vtkThreadedTaskQueueInternals::ResultQueue<R>> Results;
117
118 int NumberOfThreads;
119 std::unique_ptr<std::thread[]> Threads;
120};
121
122template <typename... Args>
123class vtkThreadedTaskQueue<void, Args...>
124{
125public:
126 vtkThreadedTaskQueue(std::function<void(Args...)> worker, bool strict_ordering = true,
127 int buffer_size = -1, int max_concurrent_tasks = -1);
129
133 void Push(Args&&... args);
134
139 bool IsEmpty() const;
140
144 void Flush();
145
146private:
148 void operator=(const vtkThreadedTaskQueue&) = delete;
149
150 std::function<void(Args...)> Worker;
151
152 std::unique_ptr<vtkThreadedTaskQueueInternals::TaskQueue<void>> Tasks;
153
154 std::condition_variable ResultsCV;
155 std::mutex NextResultIdMutex;
156 std::atomic<std::uint64_t> NextResultId;
157
158 int NumberOfThreads;
159 std::unique_ptr<std::thread[]> Threads;
160};
161
162#include "vtkThreadedTaskQueue.txx"
163
164#endif // !defined(__WRAP__)
165
166#endif
167// 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.
@ function
Definition: vtkX3D.h:255