2#include "common_std_thread.h"
14 delete_after_termination =
false;
21 delete_after_termination = _delete_after_termination;
23 std::thread*& std_thread_ptr =
reinterpret_cast<std::thread*&
>(thread_ptr);
25 std_thread_ptr =
new std::thread(&cgv::os::thread::execute_s,
this);
40 std::chrono::milliseconds dura(millisec);
41 return c.cv.wait_for(c.ul, dura) == std::cv_status::no_timeout;
61void* thread::execute_s(
void* args)
65 if (t->delete_after_termination) {
66 std::thread*& std_thread_ptr =
reinterpret_cast<std::thread*&
>(t->thread_ptr);
67 std_thread_ptr->detach();
84 std::chrono::milliseconds dura(millisec);
85 std::this_thread::sleep_for(dura);
92 std::thread* std_thread_ptr =
reinterpret_cast<std::thread*
>(thread_ptr);
93 std_thread_ptr->join();
102 std::thread*& std_thread_ptr =
reinterpret_cast<std::thread*&
>(thread_ptr);
103 std_thread_ptr->detach();
104 delete std_thread_ptr;
115 std::thread& t = *((std::thread*&) 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;
137 std::thread::id
id = std::this_thread::get_id();
138 return (
long long&) id;
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;
149class function_thread :
public thread
152 void (*func)(thread_id_type);
154 function_thread(
void (*_func)(thread_id_type))
165thread* start_in_thread(
void (*func)(thread_id_type),
bool _delete_after_termination)
167 thread* ft =
new function_thread(func);
168 ft->start(_delete_after_termination);
void run()
thread function to override
Thread class implementation that uses pthreads internally.
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
A mutex that can wake up other threads by signals sent when a condition is fulfilled.
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...