lmms
Cross-platform music production software
Toggle main menu visibility
Loading...
Searching...
No Matches
include
ThreadPool.h
Go to the documentation of this file.
1
/*
2
* ThreadPool.h
3
*
4
* Copyright (c) 2024 saker
5
*
6
* This file is part of LMMS - https://lmms.io
7
*
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU General Public
10
* License as published by the Free Software Foundation; either
11
* version 2 of the License, or (at your option) any later version.
12
*
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
17
*
18
* You should have received a copy of the GNU General Public
19
* License along with this program (see COPYING); if not, write to the
20
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
* Boston, MA 02110-1301 USA.
22
*
23
*/
24
25
#ifndef LMMS_THREAD_POOL_H
26
#define LMMS_THREAD_POOL_H
27
28
#include <atomic>
29
#include <queue>
30
#include <tuple>
31
#include <type_traits>
32
#include <vector>
33
34
#include <condition_variable>
35
#include <future>
36
#include <mutex>
37
#include <thread>
38
39
namespace
lmms
{
41
class
ThreadPool
42
{
43
public
:
46
~ThreadPool
();
47
49
template
<
typename
Fn,
typename
... Args>
50
auto
enqueue
(Fn&& fn, Args&&... args) -> std::future<std::invoke_result_t<Fn, Args...>>
51
{
52
using
ReturnType = std::invoke_result_t<Fn, Args...>;
53
54
auto
promise = std::make_shared<std::promise<ReturnType>>();
55
auto
task = [promise, fn = std::forward<Fn>(fn), args = std::make_tuple(std::forward<Args>(args)...)]
56
{
57
if
constexpr
(!std::is_same_v<ReturnType, void>)
58
{
59
promise->set_value(std::apply(fn, args));
60
return
;
61
}
62
std::apply(fn, args);
63
promise->set_value();
64
};
65
66
{
67
const
auto
lock = std::unique_lock{
m_runMutex
};
68
m_queue
.push(std::move(task));
69
}
70
71
m_runCond
.notify_one();
72
return
promise->get_future();
73
}
74
76
auto
numWorkers
() const ->
size_t
;
77
79
static auto
instance
() ->
ThreadPool
&;
80
81
private:
82
ThreadPool
(
size_t
numWorkers
);
83
void
run
();
84
std::vector<std::thread>
m_workers
;
85
std::queue<std::function<
void
()>>
m_queue
;
86
std::atomic<
bool
>
m_done
= false;
87
std::condition_variable
m_runCond
;
88
std::mutex
m_runMutex
;
89
inline static
size_t
s_numWorkers
= std::thread::hardware_concurrency();
90
};
91
}
// namespace lmms
92
93
#endif
// LMMS_THREAD_POOL_H
lmms::ThreadPool
A thread pool that can be used for asynchronous processing.
Definition
ThreadPool.h:42
lmms::ThreadPool::numWorkers
auto numWorkers() const -> size_t
Return the number of worker threads used.
Definition
ThreadPool.cpp:55
lmms::ThreadPool::m_done
std::atomic< bool > m_done
Definition
ThreadPool.h:86
lmms::ThreadPool::enqueue
auto enqueue(Fn &&fn, Args &&... args) -> std::future< std::invoke_result_t< Fn, Args... > >
Enqueue function fn with arguments args to be ran asynchronously.
Definition
ThreadPool.h:50
lmms::ThreadPool::m_queue
std::queue< std::function< void()> > m_queue
Definition
ThreadPool.h:85
lmms::ThreadPool::instance
static auto instance() -> ThreadPool &
Return the global ThreadPool instance.
Definition
ThreadPool.cpp:77
lmms::ThreadPool::m_workers
std::vector< std::thread > m_workers
Definition
ThreadPool.h:84
lmms::ThreadPool::ThreadPool
ThreadPool(size_t numWorkers)
Definition
ThreadPool.cpp:29
lmms::ThreadPool::m_runMutex
std::mutex m_runMutex
Definition
ThreadPool.h:88
lmms::ThreadPool::m_runCond
std::condition_variable m_runCond
Definition
ThreadPool.h:87
lmms::ThreadPool::s_numWorkers
static size_t s_numWorkers
Definition
ThreadPool.h:89
lmms::ThreadPool::run
void run()
Definition
ThreadPool.cpp:60
lmms
Definition
RemotePluginBase.cpp:34
Generated on
for lmms by
1.18.0