libfilezilla
thread_pool.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_THREAD_POOL_HEADER
2 #define LIBFILEZILLA_THREAD_POOL_HEADER
3 
4 #include "libfilezilla.hpp"
5 #include "mutex.hpp"
6 
7 #include <functional>
8 #include <memory>
9 #include <vector>
10 
15 namespace fz {
16 
17 class thread_pool;
18 
19 class async_task_impl;
20 
23 class FZ_PUBLIC_SYMBOL async_task final {
24 public:
25  async_task() = default;
26 
28  ~async_task();
29 
30  async_task(async_task const&) = delete;
31  async_task& operator=(async_task const&) = delete;
32 
33  async_task(async_task && other) noexcept;
34  async_task& operator=(async_task && other) noexcept;
35 
37  void join();
38 
40  explicit operator bool() const { return impl_ != nullptr; }
41 
43  void detach();
44 
45 private:
46  friend class thread_pool;
47 
48  async_task_impl* impl_{};
49 };
50 
52 class pooled_thread_impl;
53 
62 class FZ_PUBLIC_SYMBOL thread_pool final
63 {
64 public:
65  thread_pool();
66  ~thread_pool();
67 
68  thread_pool(thread_pool const&) = delete;
69  thread_pool& operator=(thread_pool const&) = delete;
70 
72  async_task spawn(std::function<void()> const& f);
73 
74 private:
75  friend class async_task;
76  friend class pooled_thread_impl;
77 
78  std::vector<pooled_thread_impl*> threads_;
79  std::vector<pooled_thread_impl*> idle_;
80  mutex m_{false};
81 };
82 
83 }
84 
85 #endif
Thread synchronization primitives: mutex, scoped_lock and condition.
Handle for asynchronous tasks.
Definition: thread_pool.hpp:23
The namespace used by libfilezilla.
Definition: apply.hpp:17
Sets some global macros and further includes string.hpp.
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:27
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:62