CEBL  2.1
TimeoutThread.cpp
Go to the documentation of this file.
1 
8 #include "TimeoutThread.hpp"
9 #include <boost/thread/xtime.hpp>
10 #include <boost/bind.hpp>
11 #include "../Exceptions.hpp"
12 
13 #include <iostream>
14 using namespace std;
15 
17 {
18  halt = false;
19  halted = true;
20  is_started = false;
21  //in milliseconds
22  timeout_length = 100;
23 
24  updater_thread = NULL;
25 }
26 
28 {
29  if(isStarted())
30  haltAndJoin();
31  if(updater_thread != NULL)
32  delete updater_thread;
33 }
34 
35 
37 {
38  if(updater_thread == NULL)
39  {
40  is_started = false;
41  return;
42  }
43  halt = true;
44  updater_thread->join();
45  delete updater_thread;
46  updater_thread = NULL;
47  is_started = false;
48 
49 }
50 
52 {
53  t->timeoutRunner();
54 }
55 
56 
58 {
59  if(!isStarted() && updater_thread == NULL)
60  {
61  halt = false;
62  halted = false;
63  is_started = true;
64  updater_thread = new boost::thread(boost::bind(&runTimeoutThread, this));
65  }
66 }
67 
68 
69 void TimeoutThread::timeoutRunner()
70 {
71 
72  while(true)
73  {
74  if(!halt)
75  {
76  try
77  {
78  this->timeoutFunction();
79  }
80  catch(exception e)
81  {
82  halt = true;
83  cerr << "Halted timeout due to exception: " << e.what() << "\n";
84  }
85  catch(const char *m)
86  {
87  halt = true;
88  cerr << "Halted timeout due to exception: " << m << "\n";
89  }
90  }
91  if(halt)
92  {
93  halted = true;
94  break;
95  }
96 
97  //sleep for a timeout_length time
98  sleep(timeout_length);
99  }
100 
101 }
102 
103 
104 void TimeoutThread::sleep(double stime)
105 {
106  boost::xtime xt;
107  boost::xtime_get(&xt,boost::TIME_UTC);
108  xt.nsec += stime * 1000000;
109  boost::thread::sleep(xt);
110 }
111 
112 
113