VTK  9.1.0
vtkSMPThreadPool.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkSMPThreadPool.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=========================================================================*/
15// .NAME vtkSMPThreadPool - A thread pool implementation using std::thread
16//
17// .SECTION Description
18// vtkSMPThreadPool class creates a thread pool of std::thread, the number
19// of thread must be specified at the initialization of the class.
20// The DoJob() method is used attributes the job to a free thread, if all
21// threads are working, the job is kept in a queue. Note that vtkSMPThreadPool
22// destructor joins threads and finish the jobs in the queue.
23
24#ifndef vtkSMPThreadPool_h
25#define vtkSMPThreadPool_h
26
27#include "vtkCommonCoreModule.h" // For export macro
28#include "vtkSystemIncludes.h"
29
30#include <condition_variable> // For std::condition_variable
31#include <functional> // For std::function
32#include <mutex> // For std::mutex
33#include <queue> // For std::queue
34#include <thread> // For std::thread
35
36namespace vtk
37{
38namespace detail
39{
40namespace smp
41{
42
43class VTKCOMMONCORE_EXPORT vtkSMPThreadPool
44{
45public:
46 explicit vtkSMPThreadPool(int ThreadNumber);
47
48 void Join();
49 void DoJob(std::function<void(void)> job);
50
51private:
52 void ThreadJob();
53
54private:
55 std::mutex Mutex;
56 bool Joining = false;
57 std::condition_variable ConditionVariable;
58 std::queue<std::function<void(void)>> JobQueue;
59 std::vector<std::thread> Threads;
60};
61
62} // namespace smp
63} // namespace detail
64} // namespace vtk
65
66#endif
void DoJob(std::function< void(void)> job)
@ function
Definition: vtkX3D.h:255
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.