blob: 1ebb52eb6b4b8d31fa83699d5ba21b0e15b10766 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2018 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: vitus@google.com (Michael Vitus)
30
31#ifndef CERES_INTERNAL_THREAD_POOL_H_
32#define CERES_INTERNAL_THREAD_POOL_H_
33
34#include <functional>
35#include <mutex>
36#include <thread>
37#include <vector>
38
39#include "ceres/concurrent_queue.h"
40
41namespace ceres {
42namespace internal {
43
44// A thread-safe thread pool with an unbounded task queue and a resizable number
45// of workers. The size of the thread pool can be increased but never decreased
46// in order to support the largest number of threads requested. The ThreadPool
47// has three states:
48//
49// (1) The thread pool size is zero. Tasks may be added to the thread pool via
50// AddTask but they will not be executed until the thread pool is resized.
51//
52// (2) The thread pool size is greater than zero. Tasks may be added to the
53// thread pool and will be executed as soon as a worker is available. The
54// thread pool may be resized while the thread pool is running.
55//
56// (3) The thread pool is destructing. The thread pool will signal all the
57// workers to stop. The workers will finish all of the tasks that have already
58// been added to the thread pool.
59//
60class ThreadPool {
61 public:
62 // Returns the maximum number of hardware threads.
63 static int MaxNumThreadsAvailable();
64
65 // Default constructor with no active threads. We allow instantiating a
66 // thread pool with no threads to support the use case of single threaded
67 // Ceres where everything will be executed on the main thread. For single
68 // threaded execution this has two benefits: avoid any overhead as threads
69 // are expensive to create, and no unused threads shown in the debugger.
70 ThreadPool();
71
72 // Instantiates a thread pool with min(MaxNumThreadsAvailable, num_threads)
73 // number of threads.
74 explicit ThreadPool(int num_threads);
75
76 // Signals the workers to stop and waits for them to finish any tasks that
77 // have been scheduled.
78 ~ThreadPool();
79
80 // Resizes the thread pool if it is currently less than the requested number
81 // of threads. The thread pool will be resized to min(MaxNumThreadsAvailable,
82 // num_threads) number of threads. Resize does not support reducing the
83 // thread pool size. If a smaller number of threads is requested, the thread
84 // pool remains the same size. The thread pool is reused within Ceres with
85 // different number of threads, and we need to ensure we can support the
86 // largest number of threads requested. It is safe to resize the thread pool
87 // while the workers are executing tasks, and the resizing is guaranteed to
88 // complete upon return.
89 void Resize(int num_threads);
90
91 // Adds a task to the queue and wakes up a blocked thread. If the thread pool
92 // size is greater than zero, then the task will be executed by a currently
93 // idle thread or when a thread becomes available. If the thread pool has no
94 // threads, then the task will never be executed and the user should use
95 // Resize() to create a non-empty thread pool.
96 void AddTask(const std::function<void()>& func);
97
98 // Returns the current size of the thread pool.
99 int Size();
100
101 private:
102 // Main loop for the threads which blocks on the task queue until work becomes
103 // available. It will return if and only if Stop has been called.
104 void ThreadMainLoop();
105
106 // Signal all the threads to stop. It does not block until the threads are
107 // finished.
108 void Stop();
109
110 // The queue that stores the units of work available for the thread pool. The
111 // task queue maintains its own thread safety.
112 ConcurrentQueue<std::function<void()>> task_queue_;
113 std::vector<std::thread> thread_pool_;
114 std::mutex thread_pool_mutex_;
115};
116
117} // namespace internal
118} // namespace ceres
119
120#endif // CERES_INTERNAL_THREAD_POOL_H_