/usr/include/libfilezilla/thread_pool.hpp is in libfilezilla-dev 0.11.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #ifndef LIBFILEZILLA_THREAD_POOL_HEADER
#define LIBFILEZILLA_THREAD_POOL_HEADER
#include "libfilezilla.hpp"
#include "mutex.hpp"
#include <functional>
#include <memory>
#include <vector>
/** \file
* \brief Declares \ref fz::thread_pool "thread_pool" and \ref fz::async_task "async_task"
*/
namespace fz {
class thread_pool;
class pooled_thread_impl;
/** \brief Handle for asynchronous tasks
*/
class FZ_PUBLIC_SYMBOL async_task final {
public:
async_task() = default;
/// If task has not been detached, calls join
~async_task();
async_task(async_task const&) = delete;
async_task& operator=(async_task const&) = delete;
async_task(async_task && other) noexcept;
async_task& operator=(async_task && other) noexcept;
/// Wait for the task to finish, adds the now idle thread back into the pool
void join();
/// Check whether it's a spawned, unjoined task.
explicit operator bool() const { return impl_ != nullptr; }
/// Detach the running thread from the task. Once done, the thread adds itself back into the pool
void detach();
private:
friend class thread_pool;
friend class pooled_thread_impl;
pooled_thread_impl* impl_{};
};
/** \brief A dumb thread-pool for asynchronous tasks
*
* If there are no idle threads, threads are created on-demand if spawning an asynchronous task.
* Once an asynchronous task finishes, the corresponding thread is kept idle until the pool is
* destroyed.
*
* Any number of tasks can be run concurrently.
*/
class FZ_PUBLIC_SYMBOL thread_pool final
{
public:
thread_pool();
~thread_pool();
thread_pool(thread_pool const&) = delete;
thread_pool& operator=(thread_pool const&) = delete;
/// Spawns a new asynchronous task.
async_task spawn(std::function<void()> const& f);
private:
friend class async_task;
friend class pooled_thread_impl;
std::vector<pooled_thread_impl*> threads_;
std::vector<pooled_thread_impl*> idle_;
mutex m_{false};
};
}
#endif
|