AWS SDK for C++  1.9.126
AWS SDK for C++
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 
18 namespace 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  protected:
49  virtual bool SubmitToThread(std::function<void()>&&) = 0;
50  };
51 
52 
57  {
58  public:
59  DefaultExecutor() : m_state(State::Free) {}
61  protected:
62  enum class State
63  {
64  Free, Locked, Shutdown
65  };
66  bool SubmitToThread(std::function<void()>&&) override;
67  void Detach(std::thread::id id);
68  std::atomic<State> m_state;
70  };
71 
72  enum class OverflowPolicy
73  {
74  QUEUE_TASKS_EVENLY_ACCROSS_THREADS,
76  };
77 
82  {
83  public:
84  PooledThreadExecutor(size_t poolSize, OverflowPolicy overflowPolicy = OverflowPolicy::QUEUE_TASKS_EVENLY_ACCROSS_THREADS);
86 
92  PooledThreadExecutor& operator =(const PooledThreadExecutor&) = delete;
94  PooledThreadExecutor& operator =(PooledThreadExecutor&&) = delete;
95 
96  protected:
97  bool SubmitToThread(std::function<void()>&&) override;
98 
99  private:
100  Aws::Queue<std::function<void()>*> m_tasks;
101  std::mutex m_queueLock;
103  Aws::Vector<ThreadTask*> m_threadTaskHandles;
104  size_t m_poolSize;
105  OverflowPolicy m_overflowPolicy;
106 
110  std::function<void()>* PopTask();
111  bool HasTasks();
112 
113  friend class ThreadTask;
114  };
115 
116 
117  } // namespace Threading
118  } // namespace Utils
119 } // namespace Aws
AWS_CORE_API
#define AWS_CORE_API
Definition: Core_EXPORTS.h:25
Aws::Utils::Threading::Semaphore
Definition: Semaphore.h:18
Aws::Utils::Threading::PooledThreadExecutor::PooledThreadExecutor
PooledThreadExecutor(PooledThreadExecutor &&)=delete
Aws::Free
AWS_CORE_API void Free(void *memoryPtr)
Aws::Utils::Threading::DefaultExecutor::Detach
void Detach(std::thread::id id)
Semaphore.h
Aws::Utils::Threading::Executor::Submit
bool Submit(Fn &&fn, Args &&... args)
Definition: Executor.h:39
Aws::Queue
std::queue< T, Deque< T > > Queue
Definition: AWSQueue.h:19
Aws::Vector
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:17
Aws::Utils::Threading::DefaultExecutor::State
State
Definition: Executor.h:63
Aws::Utils::Threading::Executor
Definition: Executor.h:31
AWSVector.h
Aws::Utils::Threading::PooledThreadExecutor::SubmitToThread
bool SubmitToThread(std::function< void()> &&) override
Aws::UnorderedMap
std::unordered_map< K, V, std::hash< K >, std::equal_to< K >, Aws::Allocator< std::pair< const K, V > > > UnorderedMap
Definition: AWSMap.h:20
Aws::Utils::Threading::Executor::SubmitToThread
virtual bool SubmitToThread(std::function< void()> &&)=0
Aws::Utils::Threading::DefaultExecutor::SubmitToThread
bool SubmitToThread(std::function< void()> &&) override
Aws::Utils::Threading::OverflowPolicy
OverflowPolicy
Definition: Executor.h:73
Aws::Utils::Threading::DefaultExecutor::m_state
std::atomic< State > m_state
Definition: Executor.h:68
Aws::Utils::Threading::PooledThreadExecutor
Definition: Executor.h:82
Aws::Utils::Threading::OverflowPolicy::QUEUE_TASKS_EVENLY_ACCROSS_THREADS
@ QUEUE_TASKS_EVENLY_ACCROSS_THREADS
Aws::Utils::Threading::PooledThreadExecutor::PooledThreadExecutor
PooledThreadExecutor(const PooledThreadExecutor &)=delete
Aws::Utils::Threading::PooledThreadExecutor::PooledThreadExecutor
PooledThreadExecutor(size_t poolSize, OverflowPolicy overflowPolicy=OverflowPolicy::QUEUE_TASKS_EVENLY_ACCROSS_THREADS)
Aws::Utils::Threading::DefaultExecutor::~DefaultExecutor
~DefaultExecutor()
Aws::Utils::Threading::PooledThreadExecutor::~PooledThreadExecutor
~PooledThreadExecutor()
AWSQueue.h
Aws::Utils::Threading::DefaultExecutor
Definition: Executor.h:57
Core_EXPORTS.h
Aws::Utils::Threading::DefaultExecutor::m_threads
Aws::UnorderedMap< std::thread::id, std::thread > m_threads
Definition: Executor.h:69
Aws
Definition: AccessManagementClient.h:15
Aws::Utils::Threading::DefaultExecutor::DefaultExecutor
DefaultExecutor()
Definition: Executor.h:59
Aws::Utils::Threading::ThreadTask
Definition: ThreadTask.h:22
AWSMap.h
Aws::Utils::Threading::Executor::~Executor
virtual ~Executor()=default