AmpGen
2.1
Toggle main menu visibility
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>
45
#include "
AmpGen/Property.h
"
46
47
namespace
AmpGen
{
48
class
ThreadPool
{
49
public
:
50
explicit
ThreadPool
(
const
size_t
&nt);
51
~ThreadPool
();
52
template
<
typename
F,
typename
... Args>
auto
enqueue
(F &&f, Args &&...args) -> std::future<
typename
std::invoke_result_t<F, Args...>>;
53
void
waitForStoppedThreads
();
54
55
private
:
56
std::vector<std::thread> m_workers;
57
std::queue<std::function<void()>> m_tasks;
58
std::mutex m_queue_mutex;
59
std::condition_variable m_condition;
60
bool
m_stop = {
false
};
61
Property<bool>
m_enableThreadPool{
this
,
"ThreadPool::Enable"
,
true
};
62
};
63
64
template
<
typename
F,
typename
... Args>
auto
ThreadPool::enqueue
(F &&f, Args &&...args) -> std::future<
typename
std::invoke_result_t<F, Args...>> {
65
using
return_type =
typename
std::invoke_result_t<F, Args...>;
66
auto
task = std::make_shared<std::packaged_task<return_type()>>(f, args...);
67
std::future<return_type> res = task->get_future();
68
if
(!m_enableThreadPool) {
69
f(args...);
70
return
res;
71
}
else
{
72
{
73
std::unique_lock<std::mutex> lock(m_queue_mutex);
74
if
(m_stop)
throw
std::runtime_error(
"enqueue on stopped ThreadPool"
);
75
m_tasks.emplace([task]() { (*task)(); });
76
}
77
m_condition.notify_one();
78
}
79
return
res;
80
}
81
}
// namespace AmpGen;
82
#endif
Property.h
AmpGen::Property
Properties are configurable characteristics of objects that will at some point inherit from a base cl...
Definition
Property.h:25
AmpGen::ThreadPool::enqueue
auto enqueue(F &&f, Args &&...args) -> std::future< typename std::invoke_result_t< F, Args... > >
Definition
ThreadPool.h:64
AmpGen::ThreadPool::ThreadPool
ThreadPool(const size_t &nt)
AmpGen::ThreadPool::~ThreadPool
~ThreadPool()
AmpGen::ThreadPool::waitForStoppedThreads
void waitForStoppedThreads()
AmpGen
Definition
AddCPConjugate.h:2
AmpGen
ThreadPool.h
Generated on
for AmpGen by
1.17.0