16 thread_ptr =
new pthread_t();
19 delete_after_termination =
false;
26 delete_after_termination = _delete_after_termination;
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);
41 pthread_cond_wait(&(pthread_cond_t&)cm.pcond, &(pthread_mutex_t&)cm.pmutex);
52 void* thread::execute_s(
void* args)
56 if (t->delete_after_termination)
72 Concurrency::wait(millisec);
74 usleep(1000*millisec);
82 pthread_join(*(pthread_t*)thread_ptr,NULL);
91 pthread_cancel(*(pthread_t*)thread_ptr);
101 pthread_join(*(pthread_t*)thread_ptr,NULL);
110 delete (pthread_t*)thread_ptr;
113 thread_id_type to_id(
const pthread_t& pt)
115 return (
const thread_id_type&) pt;
121 return (
const thread_id_type&) pthread_self();
127 return *((
const thread_id_type*)thread_ptr);
130 class function_thread :
public thread
133 void (*func)(thread_id_type);
135 function_thread(
void (*_func)(thread_id_type))
146 thread* start_in_thread(
void (*func)(thread_id_type),
bool _delete_after_termination)
148 thread* ft =
new function_thread(func);
149 ft->start(_delete_after_termination);
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
void unlock()
unlock the mutex
Definition: mutex_pthread.h:46
void start(bool _delete_after_termination=false)
start the implemented run() method (asynchronly) and destruct the thread object
Definition: thread_pthread.h:23
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
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
void wait_for_completion()
join the current thread
Definition: thread_pthread.h:98
void execute()
executes the run method
Definition: thread_pthread.h:62
void stop()
Definition: thread_pthread.h:78
static void wait(unsigned millisec)
wait the given number of milliseconds
Definition: thread_pthread.h:69
static thread_id_type get_current_thread_id()
return the id of the currently executed thread
Definition: thread_pthread.h:119
void kill()
kill a running thread
Definition: thread_pthread.h:88
virtual ~thread()
standard destructor (a running thread will be killed)
Definition: thread_pthread.h:106
virtual void run()=0
thread function to override
thread()
create the thread
Definition: thread_pthread.h:14
the cgv namespace
Definition: vr_calib.cxx:9
thread_id_type get_id() const
return id of this thread
Definition: thread_pthread.h:125