libfilezilla
thread_pool.hpp
Go to the documentation of this file.
00001 #ifndef LIBFILEZILLA_THREAD_POOL_HEADER
00002 #define LIBFILEZILLA_THREAD_POOL_HEADER
00003 
00004 #include "libfilezilla.hpp"
00005 #include "mutex.hpp"
00006 
00007 #include <functional>
00008 #include <memory>
00009 #include <vector>
00010 
00015 namespace fz {
00016 
00017 class thread_pool;
00018 class pooled_thread_impl;
00019 
00022 class FZ_PUBLIC_SYMBOL async_task final {
00023 public:
00024     async_task() = default;
00025     ~async_task();
00026 
00027     async_task(async_task const&) = delete;
00028     async_task& operator=(async_task const&) = delete;
00029 
00030     async_task(async_task && other) noexcept;
00031     async_task& operator=(async_task && other) noexcept;
00032 
00034     void join();
00035 
00037     explicit operator bool() const { return impl_ != 0; }
00038 
00039 private:
00040     friend class thread_pool;
00041     friend class pooled_thread_impl;
00042 
00043     pooled_thread_impl* impl_{};
00044 };
00045 
00054 class FZ_PUBLIC_SYMBOL thread_pool final
00055 {
00056 public:
00057     thread_pool();
00058     ~thread_pool();
00059 
00060     thread_pool(thread_pool const&) = delete;
00061     thread_pool& operator=(thread_pool const&) = delete;
00062 
00064     async_task spawn(std::function<void()> const& f);
00065 
00066 private:
00067     friend class async_task;
00068     friend class pooled_thread_impl;
00069 
00070     std::vector<pooled_thread_impl*> threads_;
00071     std::vector<pooled_thread_impl*> idle_;
00072     mutex m_{false};
00073 };
00074 
00075 }
00076 
00077 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines