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 class pooled_thread_impl;
19 
22 class FZ_PUBLIC_SYMBOL async_task final {
23 public:
24  async_task() = default;
25  ~async_task();
26 
27  async_task(async_task const&) = delete;
28  async_task& operator=(async_task const&) = delete;
29 
30  async_task(async_task && other) noexcept;
31  async_task& operator=(async_task && other) noexcept;
32 
34  void join();
35 
37  explicit operator bool() const { return impl_ != 0; }
38 
39 private:
40  friend class thread_pool;
41  friend class pooled_thread_impl;
42 
43  pooled_thread_impl* impl_{};
44 };
45 
54 class FZ_PUBLIC_SYMBOL thread_pool final
55 {
56 public:
57  thread_pool();
58  ~thread_pool();
59 
60  thread_pool(thread_pool const&) = delete;
61  thread_pool& operator=(thread_pool const&) = delete;
62 
64  async_task spawn(std::function<void()> const& f);
65 
66 private:
67  friend class async_task;
68  friend class pooled_thread_impl;
69 
70  std::vector<pooled_thread_impl*> threads_;
71  std::vector<pooled_thread_impl*> idle_;
72  mutex m_{false};
73 };
74 
75 }
76 
77 #endif
Thread synchronization primitives: mutex, scoped_lock and condition.
Handle for asynchronous tasks.
Definition: thread_pool.hpp:22
The namespace used by libfilezilla.
Definition: apply.hpp:16
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:54