FFmpegfs Fuse Multi Media Filesystem 2.16
thread_pool.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2024 Norbert Schlia (nschlia@oblivion-software.de)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * On Debian systems, the complete text of the GNU General Public License
19 * Version 3 can be found in `/usr/share/common-licenses/GPL-3'.
20 */
21
32#ifndef THREAD_POOL_H
33#define THREAD_POOL_H
34
35#pragma once
36
37#ifdef HAVE_CONFIG_H
38#include "config.h"
39#endif
40
41#include <iostream>
42#include <thread>
43#include <vector>
44#include <queue>
45#include <mutex>
46#include <condition_variable>
47#include <unistd.h>
48#include <atomic>
49#include <functional>
50
55{
56public:
57 typedef std::function<int(void)> FunctionPointer;
59public:
64 explicit thread_pool(unsigned int num_threads = std::thread::hardware_concurrency() * 4);
68 virtual ~thread_pool();
69
76 int init(unsigned int num_threads = 0);
81 void tear_down(bool silent = false);
87 bool schedule_thread(FunctionPointer && func);
92 unsigned int current_running() const;
97 unsigned int current_queued();
102 unsigned int pool_size() const;
103
104private:
113 void loop_function();
114
115protected:
116 std::vector<std::thread> m_thread_pool;
117 std::mutex m_queue_mutex;
118 std::condition_variable m_queue_cond;
119 std::queue<FunctionPointer> m_thread_queue;
120 std::atomic_bool m_queue_shutdown;
121 unsigned int m_num_threads;
122 unsigned int m_cur_threads;
123 std::atomic_uint32_t m_threads_running;
124};
125
126#endif // THREAD_POOL_H
The thread_pool class.
Definition: thread_pool.h:55
int init(unsigned int num_threads=0)
Initialise thread pool. Initialise the thread pool. Does nothing if called more than once.
Definition: thread_pool.cc:121
std::condition_variable m_queue_cond
Definition: thread_pool.h:118
unsigned int pool_size() const
Get current pool size.
Definition: thread_pool.cc:116
unsigned int m_num_threads
Definition: thread_pool.h:121
std::function< int(void)> FunctionPointer
Pointer to thread pool function.
Definition: thread_pool.h:57
void loop_function()
Start loop function.
Definition: thread_pool.cc:54
std::mutex m_queue_mutex
Definition: thread_pool.h:117
std::vector< std::thread > m_thread_pool
Definition: thread_pool.h:116
std::queue< FunctionPointer > m_thread_queue
Definition: thread_pool.h:119
static void loop_function_starter(thread_pool &tp)
Start loop function.
Definition: thread_pool.cc:49
void tear_down(bool silent=false)
Shut down the thread pool.
Definition: thread_pool.cc:144
unsigned int m_cur_threads
Definition: thread_pool.h:122
bool schedule_thread(FunctionPointer &&func)
Schedule a new thread from pool.
Definition: thread_pool.cc:82
virtual ~thread_pool()
Object destructor. Ends all threads and cleans up resources.
Definition: thread_pool.cc:44
std::atomic_uint32_t m_threads_running
Definition: thread_pool.h:123
unsigned int current_queued()
Get number of currently queued threads.
Definition: thread_pool.cc:109
thread_pool(unsigned int num_threads=std::thread::hardware_concurrency() *4)
Construct a thread_pool object.
Definition: thread_pool.cc:36
std::atomic_bool m_queue_shutdown
Definition: thread_pool.h:120
unsigned int current_running() const
Get number of currently running threads.
Definition: thread_pool.cc:104
std::unique_ptr< thread_pool > tp
Thread pool object.
Definition: fuseops.cc:102