LMMS
Loading...
Searching...
No Matches
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
39namespace lmms {
42{
43public:
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
81private:
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;
89 inline static size_t s_numWorkers = std::thread::hardware_concurrency();
90};
91} // namespace lmms
92
93#endif // LMMS_THREAD_POOL_H
pthread_mutex_t mutex
Definition Controller.C:6
static void run(LV2_Handle instance, uint32_t n_samples)
Definition bindings_test_plugin.c:112
A thread pool that can be used for asynchronous processing.
Definition ThreadPool.h:42
auto numWorkers() const -> size_t
Return the number of worker threads used.
Definition ThreadPool.cpp:55
std::atomic< bool > m_done
Definition ThreadPool.h:86
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
std::queue< std::function< void()> > m_queue
Definition ThreadPool.h:85
static auto instance() -> ThreadPool &
Return the global ThreadPool instance.
Definition ThreadPool.cpp:77
~ThreadPool()
Definition ThreadPool.cpp:40
std::vector< std::thread > m_workers
Definition ThreadPool.h:84
ThreadPool(size_t numWorkers)
Definition ThreadPool.cpp:29
std::mutex m_runMutex
Definition ThreadPool.h:88
std::condition_variable m_runCond
Definition ThreadPool.h:87
static size_t s_numWorkers
Definition ThreadPool.h:89
Definition AudioAlsa.cpp:35
Definition juce_Uuid.h:141
#define false
Definition ordinals.h:83
const char const char const char const char char * fn
Definition swell-functions.h:168
#define const
Definition zconf.h:137