2020-03-03 00:49:26 +00:00
|
|
|
/*
|
|
|
|
* Modern effects for a modern Streamer
|
|
|
|
* Copyright (C) 2020 Michael Fabian Dirks
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
2020-04-01 12:32:25 +00:00
|
|
|
#include <condition_variable>
|
2020-03-03 00:49:26 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
2020-04-01 12:32:25 +00:00
|
|
|
#include <memory>
|
2020-03-03 00:49:26 +00:00
|
|
|
#include <mutex>
|
2020-04-02 15:02:01 +00:00
|
|
|
#include <stdexcept>
|
2020-03-03 00:49:26 +00:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
namespace util {
|
2020-05-10 00:50:52 +00:00
|
|
|
typedef std::shared_ptr<void> threadpool_data_t;
|
|
|
|
typedef std::function<void(threadpool_data_t)> threadpool_callback_t;
|
2020-03-03 00:49:26 +00:00
|
|
|
|
|
|
|
class threadpool {
|
2020-05-10 00:50:52 +00:00
|
|
|
public:
|
|
|
|
class task {
|
|
|
|
protected:
|
|
|
|
std::atomic_bool _is_dead;
|
|
|
|
threadpool_callback_t _callback;
|
|
|
|
threadpool_data_t _data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
task();
|
|
|
|
task(threadpool_callback_t callback_function, threadpool_data_t data);
|
|
|
|
|
|
|
|
friend class util::threadpool;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::list<std::thread> _workers;
|
|
|
|
std::atomic_bool _worker_stop;
|
|
|
|
std::list<std::shared_ptr<::util::threadpool::task>> _tasks;
|
|
|
|
std::mutex _tasks_lock;
|
|
|
|
std::condition_variable _tasks_cv;
|
2020-03-03 00:49:26 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
threadpool();
|
|
|
|
~threadpool();
|
|
|
|
|
2020-05-10 00:50:52 +00:00
|
|
|
std::shared_ptr<::util::threadpool::task> push(threadpool_callback_t callback_function, threadpool_data_t data);
|
|
|
|
|
|
|
|
void pop(std::shared_ptr<::util::threadpool::task> work);
|
2020-03-03 00:49:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void work();
|
|
|
|
};
|
|
|
|
} // namespace util
|