Thread portable class


Gate portable class


Timer class


Pool class


Terimber 2.0


About C++


Downloads Products & Services Support Clients Open Source About



Home / Open source / Terimber 2.0

thread Class Reference

safe thread class More...

#include <thread.h>

List of all members.

Public Member Functions

 thread ()
 constructor
 ~thread ()
 destructor
thread_state get_state () const
 returns current state - thread safe
bool start ()
 starts thread, return false if thread already started
bool assign_job (const job_task &job_task)
 assigns new employer - thread safe, returns false if pointer is null or thread is not running user has to call wakeup function to activate thread
bool cancel_job ()
 cancels current job - thread safe, returns false if thread is not running
bool stop ()
 stops thread, returns false if thread already stopped
void wakeup () const
 signals thread to wakeup, if thread is in waiting state

Private Member Functions

bool inside_thread () const
 checks if the API call has been made inside this thread for instance, start, stop, assign_job, and cancel_job function calls from the same thread are ignored
 thread (const thread &x)
 copy constructor
threadoperator= (const thread &x)
 assign operator
bool change_state (thread_state new_state)
 changes current state
bool execute ()
 makes a copy of task and call employer function
bool sleep ()
 waits for either a new job (return true) or for the work to finish (return false)

Static Private Member Functions

static unsigned int __stdcall start_thread (void *data)
 thread run function

Private Attributes

job_task _job_task
 keep pointer to employer object
mutex _mtx
 mutex for thread safe access to thread state
event _ev_start
 event is activated inside thread and deactivated automatically
event _ev_wakeup
 event is activated by caller and deactivated automatically
event _ev_end
 event is activated inside thread just before exit and deactivated automatically
mutex _mtx_job
 mutex for job task access - sharing access to emplyer object
thread_state _state
 keeps thread state
HANDLE _handle
 keep thread resources


Detailed Description

safe thread class

Definition at line 107 of file thread.h.


Constructor & Destructor Documentation

thread::thread ( const thread x  )  [private]

copy constructor

prevents the copying of objects

thread::thread (  ) 

constructor

Definition at line 62 of file thread.cpp.

thread::~thread (  ) 

destructor

Definition at line 69 of file thread.cpp.

References _handle, and stop().


Member Function Documentation

BEGIN_TERIMBER_NAMESPACE unsigned int __stdcall thread::start_thread ( void *  data  )  [static, private]

thread run function

Parameters:
data  thread argument

Definition at line 40 of file thread.cpp.

References _ev_end, _ev_start, change_state(), execute(), event::signal(), sleep(), and THREAD_RUNNING.

Referenced by start().

bool thread::inside_thread (  )  const [private]

checks if the API call has been made inside this thread for instance, start, stop, assign_job, and cancel_job function calls from the same thread are ignored

Definition at line 132 of file thread.cpp.

References _handle.

Referenced by assign_job(), cancel_job(), start(), and stop().

thread& thread::operator= ( const thread x  )  [private]

assign operator

thread_state thread::get_state (  )  const

returns current state - thread safe

Definition at line 76 of file thread.cpp.

References _mtx, and _state.

Referenced by dbserver_impl::_start_thread(), and thread_creator::activate().

bool thread::assign_job ( const job_task job_task  ) 

assigns new employer - thread safe, returns false if pointer is null or thread is not running user has to call wakeup function to activate thread

Parameters:
job_task  job task

Definition at line 177 of file thread.cpp.

References _ev_wakeup, _job_task, _mtx_job, _state, inside_thread(), event::signal(), THREAD_RUNNING, THREAD_SLEEPING, and mutex_keeper::unlock().

Referenced by dbserver_impl::_start_thread(), timer::activate(), thread_creator::activate(), threadpool::on(), aiosock::on(), aiogate::on(), and aiofile::on().

bool thread::change_state ( thread_state  new_state  )  [private]

changes current state

Parameters:
new_state  new thread state

Definition at line 84 of file thread.cpp.

References _mtx, _state, THREAD_CLOSE, THREAD_RUNNING, THREAD_SLEEPING, THREAD_STARTING, and THREAD_STOPPING.

Referenced by sleep(), start(), start_thread(), and stop().

bool thread::execute (  )  [private]

bool thread::sleep (  )  [private]

waits for either a new job (return true) or for the work to finish (return false)

Definition at line 308 of file thread.cpp.

References _ev_wakeup, _job_task, _mtx_job, job_task::_timeout, change_state(), THREAD_RUNNING, THREAD_SLEEPING, mutex_keeper::unlock(), and event::wait().

Referenced by start_thread().


Member Data Documentation

keep pointer to employer object

Definition at line 165 of file thread.h.

Referenced by assign_job(), cancel_job(), execute(), and sleep().

mutex thread::_mtx [private]

mutex for thread safe access to thread state

Definition at line 166 of file thread.h.

Referenced by change_state(), and get_state().

event is activated inside thread and deactivated automatically

Definition at line 167 of file thread.h.

Referenced by start(), and start_thread().

event is activated by caller and deactivated automatically

Definition at line 168 of file thread.h.

Referenced by assign_job(), cancel_job(), sleep(), stop(), and wakeup().

event is activated inside thread just before exit and deactivated automatically

Definition at line 169 of file thread.h.

Referenced by start(), start_thread(), and stop().

mutex for job task access - sharing access to emplyer object

Definition at line 170 of file thread.h.

Referenced by assign_job(), cancel_job(), execute(), and sleep().

keeps thread state

Definition at line 171 of file thread.h.

Referenced by assign_job(), cancel_job(), change_state(), execute(), and get_state().

HANDLE thread::_handle [private]

keep thread resources

Definition at line 177 of file thread.h.

Referenced by inside_thread(), start(), stop(), and ~thread().


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


© Copyright Terimber 2003-.