cgv
thread_std_thread.h
1 #include <iostream>
2 #include "common_std_thread.h"
3 
4 namespace cgv {
5  namespace os {
6 
7 
10 {
11  thread_ptr = 0;
12  stop_request = false;
13  running = false;
14  delete_after_termination = false;
15 }
16 
18 void thread::start(bool _delete_after_termination)
19 {
20  if(!running) {
21  delete_after_termination = _delete_after_termination;
22  stop_request=false;
23  std::thread*& std_thread_ptr = reinterpret_cast<std::thread*&>(thread_ptr);
24  //if (std_thread_ptr)
25  //delete std_thread_ptr;
26  std_thread_ptr = new std::thread(&cgv::os::thread::execute_s, this);
27  running=true;
28  }
29 }
30 
32 void thread::wait_for_signal(condition_mutex& cm)
33 {
34  Condition& c = *((Condition*&) cm.pcond);
35  c.cv.wait(c.ul);
36 }
37 
40 {
41  Condition& c = *((Condition*&) cm.pcond);
42  std::chrono::milliseconds dura(millisec);
43  return c.cv.wait_for(c.ul, dura) == std::cv_status::no_timeout;
44 }
45 
48 {
49  cm.lock();
50  wait_for_signal(cm);
51  cm.unlock();
52 }
53 
56 {
57  cm.lock();
58  bool res = wait_for_signal_or_timeout(cm, millisec);
59  cm.unlock();
60  return res;
61 }
62 
63 void* thread::execute_s(void* args)
64 {
65  thread* t = (thread*)args;
66  t->execute();
67  if (t->delete_after_termination)
68  delete t;
69  return NULL;
70 }
71 
72 
73 void thread::execute()
74 {
75  run();
76  running = false;
77 }
78 
80 void thread::wait(unsigned millisec)
81 {
82  std::chrono::milliseconds dura(millisec);
83  std::this_thread::sleep_for(dura);
84 }
85 
86 void thread::stop()
87 {
88  if(running) {
89  stop_request=true;
90  std::thread* std_thread_ptr = reinterpret_cast<std::thread*>(thread_ptr);
91  std_thread_ptr->join();
92  stop_request=false;
93  }
94 }
95 
97 void thread::kill()
98 {
99  if (running) {
100  std::thread*& std_thread_ptr = reinterpret_cast<std::thread*&>(thread_ptr);
101  std_thread_ptr->detach();
102  delete std_thread_ptr;
103  std_thread_ptr = 0;
104  stop_request=false;
105  running=false;
106  }
107 }
108 
111 {
112  if (running) {
113  std::thread& t = *((std::thread*&) thread_ptr);
114  t.join();
115  }
116 }
117 
120 {
121  if(running)
122  kill();
123  if (thread_ptr) {
124  std::thread* std_thread_ptr = reinterpret_cast<std::thread*>(thread_ptr);
125  delete std_thread_ptr;
126  std_thread_ptr = 0;
127  }
128 }
129 
131 thread_id_type thread::get_current_thread_id()
132 {
133  std::thread::id id = std::this_thread::get_id();
134  return (long long&) id;
135 }
136 
138 thread_id_type thread::get_id() const
139 {
140  std::thread* std_thread_ptr = reinterpret_cast<std::thread*>(thread_ptr);
141  std::thread::id id = std_thread_ptr->get_id();
142  return (long long&) id;
143 }
144 
145 class function_thread : public thread
146 {
147 protected:
148  void (*func)(thread_id_type);
149 public:
150  function_thread(void (*_func)(thread_id_type))
151  {
152  func = _func;
153  }
154  void run()
155  {
156  func(get_id());
157  }
158 };
159 
161 thread* start_in_thread(void (*func)(thread_id_type), bool _delete_after_termination)
162 {
163  thread* ft = new function_thread(func);
164  ft->start(_delete_after_termination);
165  return ft;
166 }
167 
168  }
169 }
cgv::os::thread::wait_for_signal_with_lock
static void wait_for_signal_with_lock(condition_mutex &cm)
prefered approach to wait for signal and implemented as { cm.lock(); wait_for_signal(cm); cm....
Definition: thread_pthread.h:45
cgv::os::mutex::unlock
void unlock()
unlock the mutex
Definition: mutex_pthread.h:46
cgv::os::thread::start
void start(bool _delete_after_termination=false)
start the implemented run() method (asynchronly) and destruct the thread object
Definition: thread_pthread.h:23
cgv::os::mutex::lock
void lock()
lock the mutex (if the mutex is already locked, the caller is blocked until the mutex becomes availab...
Definition: mutex_pthread.h:40
cgv::os::thread::wait_for_signal
static void wait_for_signal(condition_mutex &cm)
sleep till the signal from the given condition_mutex is sent, lock the mutex first and unlock after w...
Definition: thread_pthread.h:39
cgv::os::thread
Definition: thread.h:39
cgv::os::thread::wait_for_completion
void wait_for_completion()
join the current thread
Definition: thread_pthread.h:98
cgv::os::condition_mutex
Definition: mutex.h:42
cgv::os::thread::execute
void execute()
executes the run method
Definition: thread_pthread.h:62
cgv::os::thread::stop
void stop()
Definition: thread_pthread.h:78
cgv::os::thread::wait_for_signal_or_timeout_with_lock
static bool wait_for_signal_or_timeout_with_lock(condition_mutex &cm, unsigned millisec)
prefered approach to wait for signal or the timeout is reached and implemented as { cm....
Definition: thread_std_thread.h:55
cgv::os::thread::wait
static void wait(unsigned millisec)
wait the given number of milliseconds
Definition: thread_pthread.h:69
cgv::os::thread::get_current_thread_id
static thread_id_type get_current_thread_id()
return the id of the currently executed thread
Definition: thread_pthread.h:119
cgv::os::thread::wait_for_signal_or_timeout
static bool wait_for_signal_or_timeout(condition_mutex &cm, unsigned millisec)
sleep till the signal from the given condition_mutex is sent or the timeout is reached,...
Definition: thread_std_thread.h:39
cgv::os::thread::kill
void kill()
kill a running thread
Definition: thread_pthread.h:88
cgv::os::thread::~thread
virtual ~thread()
standard destructor (a running thread will be killed)
Definition: thread_pthread.h:106
cgv::os::thread::run
virtual void run()=0
thread function to override
cgv::os::thread::thread
thread()
create the thread
Definition: thread_pthread.h:14
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::os::thread::get_id
thread_id_type get_id() const
return id of this thread
Definition: thread_pthread.h:125