cgv
Loading...
Searching...
No Matches
thread.h
1#pragma once
2
3#include "mutex.h"
4#include <iostream>
5
6#include "lib_begin.h"
7
8namespace cgv {
9 namespace os {
10
12typedef unsigned long long thread_id_type;
13
38class CGV_API thread
39{
40protected:
41 void *thread_ptr;
42 bool stop_request;
43 bool running;
44 bool delete_after_termination;
45 static void* execute_s(void* args);
47 void execute();
48public:
50 thread();
52 virtual ~thread();
54 void start(bool _delete_after_termination = false);
56 virtual void run()=0;
58 static void wait_for_signal(condition_mutex& cm);
62 static bool wait_for_signal_or_timeout(condition_mutex& cm, unsigned millisec);
64 static bool wait_for_signal_or_timeout_with_lock(condition_mutex& cm, unsigned millisec);
66 static void wait(unsigned millisec);
70 void stop();
72 void kill();
77 inline bool is_running() { return running; }
79 inline bool have_stop_request() { return stop_request; }
81 static thread_id_type get_current_thread_id();
83 thread_id_type get_id() const;
84};
85
87extern CGV_API thread* start_in_thread(void (*func)(thread_id_type), bool _delete_after_termination = false);
88
89 }
90}
91
92#include <cgv/config/lib_end.h>
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....
bool is_running()
return true if thread is running
Definition thread.h:77
void start(bool _delete_after_termination=false)
start the implemented run() method (asynchronly) and destruct the thread object
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....
bool have_stop_request()
check if there is a stop request
Definition thread.h:79
void kill()
kill a running thread
this header is dependency free
Definition print.h:11
A mutex that can wake up other threads by signals sent when a condition is fulfilled.
Definition mutex.h:42