AmpGen 2.1
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1#ifndef AMPGEN_THREADPOOL_H
2#define AMPGEN_THREADPOOL_H
3
4/* @class ThreadPool ThreadPool.h AmpGen/ThreadPool.h
5 * Thread pool implementation taken from https://github.com/progschj/ThreadPool
6 * Modified to allow explicit clearing of queues, and to make
7 * the constructor explicit
8 *
9 * Copyright (c) 2012 Jakob Progsch, Václav Zeman
10 *
11 * This software is provided 'as-is', without any express or implied
12 * warranty. In no event will the authors be held liable for any damages
13 * arising from the use of this software.
14 *
15 * Permission is granted to anyone to use this software for any purpose,
16 * including commercial applications, and to alter it and redistribute it
17 * freely, subject to the following restrictions:
18 *
19 * 1. The origin of this software must not be misrepresented; you must not
20 * claim that you wrote the original software. If you use this software
21 * in a product, an acknowledgment in the product documentation would be
22 * appreciated but is not required.
23 *
24 * 2. Altered source versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software.
26 *
27 * 3. This notice may not be removed or altered from any source distribution.
28 */
29
30#include <memory.h>
31#include <stddef.h>
32#include <atomic>
33#include <condition_variable>
34#include <functional>
35#include <future>
36#include <iostream>
37#include <memory>
38#include <mutex>
39#include <queue>
40#include <stdexcept>
41#include <thread>
42#include <type_traits>
43#include <utility>
44#include <vector>
46
47namespace AmpGen
48{
50 {
51 public:
52 explicit ThreadPool(const size_t& nt);
54 template<typename F, typename... Args> auto enqueue(F&& f, Args&&... args) -> std::future<typename std::invoke_result_t<F, Args...>>;
56
57 private:
58 std::vector<std::thread> m_workers;
59 std::queue<std::function<void()>> m_tasks;
60 std::mutex m_queue_mutex;
61 std::condition_variable m_condition;
62 bool m_stop={false};
63 NamedParameter<bool> m_enableThreadPool{"ThreadPool::Enable",true};
64 };
65
66 template<typename F, typename... Args> auto ThreadPool::enqueue(F&& f, Args&&... args) -> std::future<typename std::invoke_result_t<F,Args...>>
67 {
68 using return_type = typename std::invoke_result_t<F, Args...>;
69 auto task = std::make_shared< std::packaged_task<return_type()> >( f, args... );
70 std::future<return_type> res = task->get_future();
71 if(! m_enableThreadPool ) { f(args...); return res; }
72 else
73 {
74 {
75 std::unique_lock<std::mutex> lock(m_queue_mutex);
76 if(m_stop) throw std::runtime_error("enqueue on stopped ThreadPool");
77 m_tasks.emplace([task](){ (*task)(); });
78 }
79 m_condition.notify_one();
80 }
81 return res;
82 }
83} //namespace AmpGen;
84#endif
A parameter with value specified by the user at runtime, either in an options file or via the command...
ThreadPool(const size_t &nt)
void waitForStoppedThreads()
auto enqueue(F &&f, Args &&... args) -> std::future< typename std::invoke_result_t< F, Args... > >
Definition ThreadPool.h:66