OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ClientMainloopThread.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_ClientMainloopThread_h_GUID_552DB227_9D15_4B9D_E3CD_42A57C734029
26 #define INCLUDED_ClientMainloopThread_h_GUID_552DB227_9D15_4B9D_E3CD_42A57C734029
27 
28 // Internal Includes
29 #include "ClientMainloop.h"
30 
31 // Library/third-party includes
32 #include <boost/thread/thread.hpp>
33 #include <boost/date_time/posix_time/posix_time.hpp>
34 #include <boost/chrono.hpp>
35 
36 // Standard includes
37 #include <stdexcept>
38 
39 static const auto SLEEP_TIME = boost::posix_time::milliseconds(1);
40 
41 class ClientMainloopThread : boost::noncopyable {
42  public:
43  typedef ClientMainloop::mutex_type mutex_type;
44  typedef ClientMainloop::lock_type lock_type;
46  bool startNow = false)
47  : m_run(false), m_started(false), m_mainloop(ctx) {
48  if (startNow) {
49  start();
50  }
51  }
52  void start() {
53  if (m_run || m_started) {
54  throw std::logic_error(
55  "Can't start if it's already started or if this is a re-start");
56  }
57  m_started = true;
58  m_run = true;
59  m_thread = boost::thread([&] {
60  while (m_run) {
61  oneLoop();
62  }
63  });
64  }
65 
66  void oneLoop() {
67  m_mainloop.mainloop();
68  boost::this_thread::sleep(SLEEP_TIME);
69  }
70 
71  template <typename T>
72  void loopForDuration(T duration = boost::chrono::seconds(2)) {
73  typedef boost::chrono::steady_clock clock;
74  auto start = clock::now();
75  do {
76  oneLoop();
77  } while (clock::now() - start < duration);
78  }
79 
81  m_run = false;
82  m_thread.join();
83  }
84  mutex_type &getMutex() { return m_mainloop.getMutex(); }
85 
86  private:
87  volatile bool m_run;
88  bool m_started;
89  ClientMainloop m_mainloop;
90  boost::thread m_thread;
91 };
92 
93 #endif // INCLUDED_ClientMainloopThread_h_GUID_552DB227_9D15_4B9D_E3CD_42A57C734029
Client context object: Create and keep one in your application. Handles lifetime management and provi...
Definition: Context_decl.h:57
double duration(TimeValue const &a, TimeValue const &b)
Get a double containing seconds between the time points.
Definition: TimeValue.h:62
Simple class to handle running a client mainloop in another thread, but easily pausable.