YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
fair_thread_pool.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <yaclib/exe/job.hpp>
6
7#include <cstdint>
8#include <vector>
9#include <yaclib_std/condition_variable>
10#include <yaclib_std/mutex>
11#include <yaclib_std/thread>
12
13namespace yaclib {
14
15/**
16 * TODO(kononovk) Doxygen docs
17 */
18class FairThreadPool : public IExecutor {
19 public:
20 explicit FairThreadPool(std::uint64_t threads = yaclib_std::thread::hardware_concurrency());
21
22 ~FairThreadPool() noexcept override;
23
24 Type Tag() const noexcept final;
25
26 bool Alive() const noexcept final;
27
28 void Submit(Job& task) noexcept final;
29
30 void SoftStop() noexcept;
31
32 void Stop() noexcept;
33
34 void HardStop() noexcept;
35
36 /**
37 * TODO(kononovk) Rename to Join
38 */
39 void Wait() noexcept;
40
41 private:
42 void Loop() noexcept;
43
44 [[nodiscard]] bool WasStop() const noexcept;
45 [[nodiscard]] bool WantStop() const noexcept;
46 [[nodiscard]] bool NoJobs() const noexcept;
47
48 void Stop(std::unique_lock<yaclib_std::mutex>&& lock) noexcept;
49
50 std::vector<yaclib_std::thread> _workers;
51 mutable yaclib_std::mutex _m;
53 detail::List _jobs;
54 std::uint64_t _jobs_count;
55};
56
57IntrusivePtr<FairThreadPool> MakeFairThreadPool(std::uint64_t threads = yaclib_std::thread::hardware_concurrency());
58
59} // namespace yaclib
TODO(kononovk) Doxygen docs.
void Submit(Job &task) noexcept final
Submit given job.
bool Alive() const noexcept final
Return true if executor still alive, that means job passed to submit will be Call.
Type Tag() const noexcept final
Return type of this executor.
~FairThreadPool() noexcept override
void Wait() noexcept
TODO(kononovk) Rename to Join.
A intrusive pointer to objects with an embedded reference count.
Callable that can be executed in an IExecutor.
Definition job.hpp:12
std::mutex mutex
Definition mutex.hpp:28
std::condition_variable condition_variable
IntrusivePtr< FairThreadPool > MakeFairThreadPool(std::uint64_t threads=yaclib_std::thread::hardware_concurrency())
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25