cgv
|
Thread class implementation that uses pthreads internally. More...
#include <thread.h>
Public Member Functions | |
thread () | |
create the thread | |
virtual | ~thread () |
standard destructor (a running thread will be killed) | |
void | start (bool _delete_after_termination=false) |
start the implemented run() method (asynchronly) and destruct the thread object | |
virtual void | run ()=0 |
thread function to override | |
void | stop () |
try to stop the thread execution via indicating a stop request. | |
void | kill () |
kill a running thread | |
void | wait_for_completion () |
the thread is interpreted as a slave thread and started from another master thread. | |
bool | is_running () |
return true if thread is running | |
bool | have_stop_request () |
check if there is a stop request | |
thread_id_type | get_id () const |
return id of this thread | |
Static Public Member Functions | |
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 waiting | |
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.unlock(); } | |
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, lock the mutex first and unlock after waiting | |
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.lock(); wait_for_signal_or_timeout(cm,millisec); cm.unlock(); } | |
static void | wait (unsigned millisec) |
wait the given number of milliseconds | |
static thread_id_type | get_current_thread_id () |
return the id of the currently executed thread | |
Protected Member Functions | |
void | execute () |
executes the run method | |
Static Protected Member Functions | |
static void * | execute_s (void *args) |
Protected Attributes | |
void * | thread_ptr |
bool | stop_request |
bool | running |
bool | delete_after_termination |
Thread class implementation that uses pthreads internally.
To create and run your own thread, follow this example: \begincode class mythread : thread {
void run() {
no external stop request? while(no_stop_request()) { std::cout << "abc" << std::endl; } }
};
mythread t1;
t1.start(); ... t1.stop();
cgv::os::thread::thread | ( | ) |
create the thread
Definition at line 14 of file thread_pthread.h.
|
virtual |
standard destructor (a running thread will be killed)
Definition at line 106 of file thread_pthread.h.
References kill().
|
protected |
|
staticprotected |
Definition at line 52 of file thread_pthread.h.
|
static |
return the id of the currently executed thread
Definition at line 119 of file thread_pthread.h.
thread_id_type cgv::os::thread::get_id | ( | ) | const |
return id of this thread
Definition at line 125 of file thread_pthread.h.
Referenced by cgv::os::function_thread::run().
|
inline |
check if there is a stop request
Definition at line 79 of file thread.h.
Referenced by cgv::os::queued_output_thread::run(), and cgv::os::queued_input_thread::run().
|
inline |
void cgv::os::thread::kill | ( | ) |
kill a running thread
Definition at line 88 of file thread_pthread.h.
Referenced by ~thread(), and cgv::os::web_server_thread::~web_server_thread().
|
pure virtual |
thread function to override
Implemented in cgv::os::queued_output_thread, cgv::os::queued_input_thread, cgv::os::function_thread, cgv::os::function_thread, and cgv::os::web_server_thread.
Referenced by execute().
void cgv::os::thread::start | ( | bool | _delete_after_termination = false | ) |
start the implemented run() method (asynchronly) and destruct the thread object
start the implemented run() method (asynchronly)
Definition at line 23 of file thread_pthread.h.
Referenced by cgv::os::web_server_thread::start().
void cgv::os::thread::stop | ( | ) |
try to stop the thread execution via indicating a stop request.
The existence of a stop request can be recognized by the no_stop_request() method. This test should be done periodically within the implementation of the run() method, e.g. to leave the execution loop in a clean way.
Definition at line 78 of file thread_pthread.h.
|
static |
wait the given number of milliseconds
Definition at line 69 of file thread_pthread.h.
Referenced by cgv::os::queued_output_thread::run(), and cgv::os::queued_input_thread::run().
void cgv::os::thread::wait_for_completion | ( | ) |
the thread is interpreted as a slave thread and started from another master thread.
join the current thread
This method is called from the master thread in order to wait for termination of the slave thread.
Definition at line 98 of file thread_pthread.h.
|
static |
sleep till the signal from the given condition_mutex is sent, lock the mutex first and unlock after waiting
sleep till the signal from the given condition_mutex is sent
Definition at line 39 of file thread_pthread.h.
Referenced by wait_for_signal_with_lock().
|
static |
sleep till the signal from the given condition_mutex is sent or the timeout is reached, lock the mutex first and unlock after waiting
Definition at line 37 of file thread_std_thread.h.
Referenced by wait_for_signal_or_timeout_with_lock().
|
static |
prefered approach to wait for signal or the timeout is reached and implemented as { cm.lock(); wait_for_signal_or_timeout(cm,millisec); cm.unlock(); }
Definition at line 53 of file thread_std_thread.h.
References cgv::os::mutex::lock(), cgv::os::mutex::unlock(), and wait_for_signal_or_timeout().
|
static |
prefered approach to wait for signal and implemented as { cm.lock(); wait_for_signal(cm); cm.unlock(); }
Definition at line 45 of file thread_pthread.h.
References cgv::os::mutex::lock(), cgv::os::mutex::unlock(), and wait_for_signal().