cgv
thread_pthread.h
1 #include "thread.h"
2 #include <pthread.h>
3 #include <iostream>
4 #ifdef _WIN32
5 #include <concrt.h>
6 #else
7 #include <unistd.h>
8 #endif
9 
10 namespace cgv {
11  namespace os {
12 
15 {
16  thread_ptr = new pthread_t();
17  stop_request = false;
18  running = false;
19  delete_after_termination = false;
20 }
21 
23 void thread::start(bool _delete_after_termination)
24 {
25  if(!running) {
26  delete_after_termination = _delete_after_termination;
27  stop_request=false;
28  pthread_attr_t attr;
29  pthread_attr_init(&attr);
30  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
31  if(pthread_create((pthread_t*)thread_ptr,&attr,execute_s,this))
32  std::cerr << "error: starting thread" <<std::endl;
33  pthread_attr_destroy(&attr);
34  running=true;
35  }
36 }
37 
40 {
41  pthread_cond_wait(&(pthread_cond_t&)cm.pcond, &(pthread_mutex_t&)cm.pmutex);
42 }
43 
46 {
47  cm.lock();
48  wait_for_signal(cm);
49  cm.unlock();
50 }
51 
52 void* thread::execute_s(void* args)
53 {
54  thread* t = (thread*)args;
55  t->execute();
56  if (t->delete_after_termination)
57  delete t;
58  return NULL;
59 }
60 
61 
63 {
64  run();
65  running = false;
66 }
67 
69 void thread::wait(unsigned millisec)
70 {
71 #ifdef _WIN32
72  Concurrency::wait(millisec);
73 #else
74  usleep(1000*millisec);
75 #endif
76 }
77 
79 {
80  if(running) {
81  stop_request=true;
82  pthread_join(*(pthread_t*)thread_ptr,NULL);
83  stop_request=false;
84  }
85 }
86 
89 {
90  if (running) {
91  pthread_cancel(*(pthread_t*)thread_ptr);
92  stop_request=false;
93  running=false;
94  }
95 }
96 
99 {
100  if (running)
101  pthread_join(*(pthread_t*)thread_ptr,NULL);
102 
103 }
104 
107 {
108  if(running)
109  kill();
110  delete (pthread_t*)thread_ptr;
111 }
112 
113 thread_id_type to_id(const pthread_t& pt)
114 {
115  return (const thread_id_type&) pt;
116 }
117 
120 {
121  return (const thread_id_type&) pthread_self();
122 }
123 
125 thread_id_type thread::get_id() const
126 {
127  return *((const thread_id_type*)thread_ptr);
128 }
129 
130 class function_thread : public thread
131 {
132 protected:
133  void (*func)(thread_id_type);
134 public:
135  function_thread(void (*_func)(thread_id_type))
136  {
137  func = _func;
138  }
139  void run()
140  {
141  func(get_id());
142  }
143 };
144 
146 thread* start_in_thread(void (*func)(thread_id_type), bool _delete_after_termination)
147 {
148  thread* ft = new function_thread(func);
149  ft->start(_delete_after_termination);
150  return ft;
151 }
152 
153  }
154 }
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
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::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