BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
ThreadWrapperAsyncWork.h
1 #pragma once
2 
3 #include <functional>
4 
5 #include <Poco/Thread.h>
6 
7 #include "util/AbstractAsyncWork.h"
8 
9 namespace BeeeOn {
10 
16 template <typename Result = Poco::Void>
17 class ThreadWrapperAsyncWork : public AbstractAsyncWork<Result> {
18 public:
19  typedef Poco::SharedPtr<ThreadWrapperAsyncWork<Result>> Ptr;
20 
21  ThreadWrapperAsyncWork(Poco::Thread &thread);
22 
23  bool tryJoin(const Poco::Timespan &timeout) override;
24  void cancel() override;
25 
26  Poco::Thread &thread();
27 
28 private:
29  Poco::Thread &m_thread;
30 };
31 
32 template <typename Result>
34  Poco::Thread &thread):
35  m_thread(thread)
36 {
37 }
38 
39 template <typename Result>
40 bool ThreadWrapperAsyncWork<Result>::tryJoin(const Poco::Timespan &timeout)
41 {
42  return m_thread.tryJoin(timeout.totalMilliseconds());
43 }
44 
45 template <typename Result>
47 {
48  m_thread.join();
49 }
50 
51 template <typename Result>
53 {
54  return m_thread;
55 }
56 
57 }
bool tryJoin(const Poco::Timespan &timeout) override
Definition: ThreadWrapperAsyncWork.h:40
AbstractAsyncWork provides a generic implementation of method result() and a supplementary method set...
Definition: AbstractAsyncWork.h:17
Adapter of Poco::Thread to AsyncWork interface. The Thread has no general way how to be stopped and t...
Definition: ThreadWrapperAsyncWork.h:17
void cancel() override
Cancel the operation this object represents. It should exit immediatelly or at least as soon as possi...
Definition: ThreadWrapperAsyncWork.h:46