cgv
Loading...
Searching...
No Matches
cgv::os::thread Class Referenceabstract

Thread class implementation that uses pthreads internally. More...

#include <thread.h>

Inheritance diagram for cgv::os::thread:
cgv::os::function_thread cgv::os::function_thread cgv::os::queued_input_thread cgv::os::queued_output_thread cgv::os::web_server_thread cgv::os::named_pipe_input_thread cgv::os::pipe_input_thread cgv::os::named_pipe_output_thread cgv::os::pipe_output_thread

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
 

Detailed Description

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();

Definition at line 38 of file thread.h.

Constructor & Destructor Documentation

◆ thread()

cgv::os::thread::thread ( )

create the thread

Definition at line 14 of file thread_pthread.h.

◆ ~thread()

cgv::os::thread::~thread ( )
virtual

standard destructor (a running thread will be killed)

Definition at line 106 of file thread_pthread.h.

References kill().

Member Function Documentation

◆ execute()

void cgv::os::thread::execute ( )
protected

executes the run method

Definition at line 62 of file thread_pthread.h.

References run().

◆ execute_s()

void * cgv::os::thread::execute_s ( void *  args)
staticprotected

Definition at line 52 of file thread_pthread.h.

◆ get_current_thread_id()

thread_id_type cgv::os::thread::get_current_thread_id ( )
static

return the id of the currently executed thread

Definition at line 119 of file thread_pthread.h.

◆ get_id()

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().

◆ have_stop_request()

bool cgv::os::thread::have_stop_request ( )
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().

◆ is_running()

bool cgv::os::thread::is_running ( )
inline

return true if thread is running

Definition at line 77 of file thread.h.

◆ kill()

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().

◆ run()

virtual void cgv::os::thread::run ( )
pure virtual

◆ start()

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().

◆ stop()

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.

◆ wait()

void cgv::os::thread::wait ( unsigned  millisec)
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().

◆ wait_for_completion()

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.

◆ wait_for_signal()

void cgv::os::thread::wait_for_signal ( condition_mutex cm)
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().

◆ wait_for_signal_or_timeout()

bool cgv::os::thread::wait_for_signal_or_timeout ( condition_mutex cm,
unsigned  millisec 
)
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().

◆ wait_for_signal_or_timeout_with_lock()

bool cgv::os::thread::wait_for_signal_or_timeout_with_lock ( condition_mutex cm,
unsigned  millisec 
)
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().

◆ wait_for_signal_with_lock()

void cgv::os::thread::wait_for_signal_with_lock ( condition_mutex cm)
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().

Member Data Documentation

◆ delete_after_termination

bool cgv::os::thread::delete_after_termination
protected

Definition at line 44 of file thread.h.

◆ running

bool cgv::os::thread::running
protected

Definition at line 43 of file thread.h.

◆ stop_request

bool cgv::os::thread::stop_request
protected

Definition at line 42 of file thread.h.

◆ thread_ptr

void* cgv::os::thread::thread_ptr
protected

Definition at line 41 of file thread.h.


The documentation for this class was generated from the following files: