TECA
The Toolkit for Extreme Climate Analysis
teca_owned_future.h
1 #ifndef teca_owned_future_h
2 #define teca_owned_future_h
3 
4 #include <future>
5 #include <thread>
6 
7 /// a future that is owned by a single thread
8 template <typename data_t>
10 {
11  owned_future() = delete;
12  owned_future(const owned_future&) = delete;
13  void operator=(const owned_future&) = delete;
14 
15  /// move construct from another instance
17  m_future(std::move(other.m_future)), m_owner(other.m_owner) {}
18 
19  /// move contruct from std::future
20  owned_future(std::future<data_t> &&future) :
21  m_future(std::move(future)), m_owner(std::this_thread::get_id()) {}
22 
23  /// move assign form anotehr instance
24  void operator=(owned_future &&other)
25  {
26  m_future = std::move(other.m_future);
27  m_owner = other.m_owner;
28  }
29 
30  /// true if this future belongs to the calling thread
31  bool owner()
32  {
33  return std::this_thread::get_id() == m_owner;
34  }
35 
36  /// access the managed future's data
37  data_t &get() { return m_future.get(); }
38 
39  std::future<data_t> m_future; ///< the managed future
40  std::thread::id m_owner; ///< the thread id of the thread which created the future
41 };
42 
43 #endif
a future that is owned by a single thread
Definition: teca_owned_future.h:10
data_t & get()
access the managed future's data
Definition: teca_owned_future.h:37
owned_future(owned_future &&other)
move construct from another instance
Definition: teca_owned_future.h:16
bool owner()
true if this future belongs to the calling thread
Definition: teca_owned_future.h:31
owned_future(std::future< data_t > &&future)
move contruct from std::future
Definition: teca_owned_future.h:20
std::future< data_t > m_future
the managed future
Definition: teca_owned_future.h:39
void operator=(owned_future &&other)
move assign form anotehr instance
Definition: teca_owned_future.h:24
std::thread::id m_owner
the thread id of the thread which created the future
Definition: teca_owned_future.h:40