cgv
Loading...
Searching...
No Matches
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
10namespace 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
23void 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
51
52void* 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
69void 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
113thread_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
125thread_id_type thread::get_id() const
126{
127 return *((const thread_id_type*)thread_ptr);
128}
129
131{
132protected:
133 void (*func)(thread_id_type);
134public:
135 function_thread(void (*_func)(thread_id_type))
136 {
137 func = _func;
138 }
139 void run()
140 {
141 func(get_id());
142 }
143};
144
146thread* 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}
void run()
thread function to override
Thread class implementation that uses pthreads internally.
Definition thread.h:39
virtual void run()=0
thread function to override
static void wait(unsigned millisec)
wait the given number of milliseconds
void execute()
executes the run method
void stop()
try to stop the thread execution via indicating a stop request.
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...
thread()
create the thread
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....
thread_id_type get_id() const
return id of this thread
void start(bool _delete_after_termination=false)
start the implemented run() method (asynchronly) and destruct the thread object
virtual ~thread()
standard destructor (a running thread will be killed)
void wait_for_completion()
the thread is interpreted as a slave thread and started from another master thread.
static thread_id_type get_current_thread_id()
return the id of the currently executed thread
void kill()
kill a running thread
the cgv namespace
Definition print.h:11
A mutex that can wake up other threads by signals sent when a condition is fulfilled.
Definition mutex.h:42
void unlock()
unlock the mutex
void lock()
lock the mutex (if the mutex is already locked, the caller is blocked until the mutex becomes availab...