cgv
Loading...
Searching...
No Matches
thread_std_thread.h
1#include <iostream>
2#include "common_std_thread.h"
3
4namespace 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
18void 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 running = true;
25 std_thread_ptr = new std::thread(&cgv::os::thread::execute_s, this);
26 }
27}
28
30void thread::wait_for_signal(condition_mutex& cm)
31{
32 Condition& c = *((Condition*&) cm.pcond);
33 c.cv.wait(c.ul);
34}
35
38{
39 Condition& c = *((Condition*&) cm.pcond);
40 std::chrono::milliseconds dura(millisec);
41 return c.cv.wait_for(c.ul, dura) == std::cv_status::no_timeout;
42}
43
46{
47 cm.lock();
48 wait_for_signal(cm);
49 cm.unlock();
50}
51
54{
55 cm.lock();
56 bool res = wait_for_signal_or_timeout(cm, millisec);
57 cm.unlock();
58 return res;
59}
60
61void* thread::execute_s(void* args)
62{
63 thread* t = (thread*)args;
64 t->execute();
65 if (t->delete_after_termination) {
66 std::thread*& std_thread_ptr = reinterpret_cast<std::thread*&>(t->thread_ptr);
67 std_thread_ptr->detach();
68 std_thread_ptr = 0;
69 delete t;
70 }
71 return NULL;
72}
73
74
75void thread::execute()
76{
77 run();
78 running = false;
79}
80
82void thread::wait(unsigned millisec)
83{
84 std::chrono::milliseconds dura(millisec);
85 std::this_thread::sleep_for(dura);
86}
87
88void thread::stop()
89{
90 if(running) {
91 stop_request=true;
92 std::thread* std_thread_ptr = reinterpret_cast<std::thread*>(thread_ptr);
93 std_thread_ptr->join();
94 stop_request=false;
95 }
96}
97
99void thread::kill()
100{
101 if (running) {
102 std::thread*& std_thread_ptr = reinterpret_cast<std::thread*&>(thread_ptr);
103 std_thread_ptr->detach();
104 delete std_thread_ptr;
105 std_thread_ptr = 0;
106 stop_request=false;
107 running=false;
108 }
109}
110
113{
114 if (running) {
115 std::thread& t = *((std::thread*&) thread_ptr);
116 t.join();
117 }
118}
119
122{
123 if(running)
124 kill();
125 if (thread_ptr) {
126 std::thread*& std_thread_ptr = reinterpret_cast<std::thread*&>(thread_ptr);
127 if (std_thread_ptr->joinable())
128 std_thread_ptr->detach();
129 delete std_thread_ptr;
130 std_thread_ptr = 0;
131 }
132}
133
135thread_id_type thread::get_current_thread_id()
136{
137 std::thread::id id = std::this_thread::get_id();
138 return (long long&) id;
139}
140
142thread_id_type thread::get_id() const
143{
144 std::thread* std_thread_ptr = reinterpret_cast<std::thread*>(thread_ptr);
145 std::thread::id id = std_thread_ptr->get_id();
146 return (long long&) id;
147}
148
149class function_thread : public thread
150{
151protected:
152 void (*func)(thread_id_type);
153public:
154 function_thread(void (*_func)(thread_id_type))
155 {
156 func = _func;
157 }
158 void run()
159 {
160 func(get_id());
161 }
162};
163
165thread* start_in_thread(void (*func)(thread_id_type), bool _delete_after_termination)
166{
167 thread* ft = new function_thread(func);
168 ft->start(_delete_after_termination);
169 return ft;
170}
171
172 }
173}
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)
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,...
void wait_for_completion()
the thread is interpreted as a slave thread and started from another master thread.
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....
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...