AWS SDK for C++
AWS SDK for C++
Loading...
Searching...
No Matches
Executor.h
Go to the documentation of this file.
1
6#pragma once
7
13#include <functional>
14#include <future>
15#include <mutex>
16#include <atomic>
17
18namespace Aws
19{
20 namespace Utils
21 {
22 namespace Threading
23 {
24 class ThreadTask;
25
31 {
32 public:
33 virtual ~Executor() = default;
34
38 template<class Fn, class ... Args>
39 bool Submit(Fn&& fn, Args&& ... args)
40 {
41 std::function<void()> callable{ std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...) };
42 return SubmitToThread(std::move(callable));
43 }
44
45 /* explicit _overload_ of the template function above to avoid template bloat */
46 bool Submit(std::function<void()>&& callable)
47 {
48 return SubmitToThread(std::move(callable));
49 }
50
51 protected:
55 virtual bool SubmitToThread(std::function<void()>&&) = 0;
56 };
57
58
63 {
64 public:
65 DefaultExecutor() : m_state(State::Free) {}
67 protected:
68 enum class State
69 {
70 Free, Locked, Shutdown
71 };
72 bool SubmitToThread(std::function<void()>&&) override;
73 void Detach(std::thread::id id);
74 std::atomic<State> m_state;
76 };
77
78 enum class OverflowPolicy
79 {
82 };
83
88 {
89 public:
90 PooledThreadExecutor(size_t poolSize, OverflowPolicy overflowPolicy = OverflowPolicy::QUEUE_TASKS_EVENLY_ACROSS_THREADS);
92
98 PooledThreadExecutor& operator =(const PooledThreadExecutor&) = delete;
101
102 protected:
103 bool SubmitToThread(std::function<void()>&&) override;
104
105 private:
106 Aws::Queue<std::function<void()>*> m_tasks;
107 std::mutex m_queueLock;
109 Aws::Vector<ThreadTask*> m_threadTaskHandles;
110 size_t m_poolSize;
111 OverflowPolicy m_overflowPolicy;
112
116 std::function<void()>* PopTask();
117 bool HasTasks();
118
119 friend class ThreadTask;
120 };
121
122
123 } // namespace Threading
124 } // namespace Utils
125} // namespace Aws
#define AWS_CORE_API
Definition: Core_EXPORTS.h:26
Aws::UnorderedMap< std::thread::id, std::thread > m_threads
Definition: Executor.h:75
bool SubmitToThread(std::function< void()> &&) override
void Detach(std::thread::id id)
std::atomic< State > m_state
Definition: Executor.h:74
bool Submit(Fn &&fn, Args &&... args)
Definition: Executor.h:39
bool Submit(std::function< void()> &&callable)
Definition: Executor.h:46
virtual bool SubmitToThread(std::function< void()> &&)=0
bool SubmitToThread(std::function< void()> &&) override
PooledThreadExecutor(size_t poolSize, OverflowPolicy overflowPolicy=OverflowPolicy::QUEUE_TASKS_EVENLY_ACROSS_THREADS)
PooledThreadExecutor(PooledThreadExecutor &&)=delete
PooledThreadExecutor(const PooledThreadExecutor &)=delete
std::queue< T, Deque< T > > Queue
Definition: AWSQueue.h:19
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:17
AWS_CORE_API void Free(void *memoryPtr)
std::unordered_map< K, V, std::hash< K >, std::equal_to< K >, Aws::Allocator< std::pair< const K, V > > > UnorderedMap
Definition: AWSMap.h:20