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 
27  ~async_task();
28 
29  async_task(async_task const&) = delete;
30  async_task& operator=(async_task const&) = delete;
31 
32  async_task(async_task && other) noexcept;
33  async_task& operator=(async_task && other) noexcept;
34 
36  void join();
37 
39  explicit operator bool() const { return impl_ != nullptr; }
40 
42  void detach();
43 
44 private:
45  friend class thread_pool;
46  friend class pooled_thread_impl;
47 
48  pooled_thread_impl* impl_{};
49 };
50 
59 class FZ_PUBLIC_SYMBOL thread_pool final
60 {
61 public:
62  thread_pool();
63  ~thread_pool();
64 
65  thread_pool(thread_pool const&) = delete;
66  thread_pool& operator=(thread_pool const&) = delete;
67 
69  async_task spawn(std::function<void()> const& f);
70 
71 private:
72  friend class async_task;
73  friend class pooled_thread_impl;
74 
75  std::vector<pooled_thread_impl*> threads_;
76  std::vector<pooled_thread_impl*> idle_;
77  mutex m_{false};
78 };
79 
80 }
81 
82 #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:59