Posix thread class. More...
#include <thread.h>
Public Types | |
enum | errors { BAD_EXCEPTION = -1, DEFAULT_MAIN = -2 } |
Public Member Functions | |
Thread (bool delete_after_main=false, unsigned int min_debug_level=0, Debugable *debug_master=0) | |
Constructor. | |
virtual | ~Thread () |
Destructor. | |
int | start () |
Start up thread, executing the main function of this Thread object. | |
virtual int | main () |
Main function of thread. | |
int | join () |
Wait for a non-detached thread to finish. | |
void | wait () |
Wait for a thread to stop running. | |
virtual void | kill (int signal=SIGUSR1) |
Kill a thread. | |
int | status () const |
Return exit status of this thread. | |
pthread_t | id () const |
Return posix thread id of this thread. | |
std::string | sid () const |
String representation of thread id of this thread. | |
int | detach () const |
Detach this thread. | |
bool | is_self () const |
Is this thread the currently running thread? This function is 'delete-after-main safe'. | |
bool | running () const |
Is this thread running? | |
pthread_t | joined_by () const |
Has this thread been joined? | |
pthread_t | detached_by () const |
Has this thread been detached? | |
Static Public Member Functions | |
static Thread * | self () |
Return pointer to currently running thread or 0. | |
static pthread_t | self_id () throw () |
Return id (pthread_t ) of the currently running thread. | |
static void | sleep (size_t millisecs) |
Safe version of sleep which uses a 'dummy' Monitor::wait. | |
static size_t | wait_for_all () |
Wait for all threads to have finished. | |
static int | sig_set_handler (int signal=SIGUSR1, void(*handler)(int)=Dv::Thread::Thread::sig_handler, int sigaction_flags=0) |
Set signal handler for all threads (in this process). | |
static void | sig_handler (int signal) |
Default signal handler. | |
static int | sig_block (int signal, bool unblock=false) |
Block or unblock a signal for the currently running thread. | |
static int | sig_unblock (int signal) |
Unblock a signal for the currently running thread. | |
Protected Member Functions | |
void | exit (int status) |
Finish this thread, with status. | |
int | killed () const |
Protected Attributes | |
volatile sig_atomic_t | killed_ |
Set by default signal handler. | |
Private Member Functions | |
bool | delete_after_main () const |
Will this thread object be deleted after Thread::main() has completed? | |
bool | at_end () const |
Thread (const Thread &) | |
Forbid cctor. | |
Thread & | operator= (const Thread &) |
Forbid assignment. | |
void | set_status (int status) |
Set the exit status of this thread. | |
Static Private Member Functions | |
static void * | run_thread (Dv::Thread::Thread *t) |
Startup function wrapping around t->main, passed to pthread_create. | |
Private Attributes | |
pthread_t | id_ |
Posix thread id. | |
pthread_attr_t | attributes_ |
Posix thread attributes. | |
int | exit_status_ |
Exit status of this thread. | |
bool | delete_after_main_ |
Should this object be deleted after Thread::main has completed? | |
pthread_t | joiner_ |
pthread_t id of thread that has joined this thread or 0 | |
pthread_t | detacher_ |
pthread_t id of thread that has detached this thread or 0 | |
int | cancel_state_ |
we disable this | |
Barrier | barrier_ |
private barrier, used to synchronize parent and new thread | |
bool | at_end_ |
set at end of Dv::Thread::Thread::run_thread | |
Friends | |
class | Threads |
Singleton class that keeps the relation between Dv::Thread::Thread objects and corresponding pthread_t objects (i.e. | |
int | start (Thread &t) |
Posix thread class.
Dv::Thread::Thread::Thread | ( | bool | delete_after_main = false , |
|
unsigned int | min_debug_level = 0 , |
|||
Debugable * | debug_master = 0 | |||
) |
Constructor.
Note that there are 2 types of debug: for internal debugging of the dvthread library: use Dv::Thread::thread_debug . For debugging of derived classes write e.g. to t.log().
delete_after_main | if true, delete(t) will be executed after t->main() has completed. Since joining such a thread may not be possible (the Dv::Thread::Thread object may have been auto-deleted by the time another thread wants to join it), such threads are automatically detached by Dv::Thread::Thread::start. | |
min_debug_level | if a debug_master is connected, statements such as
log(4) << FUNCTION_S << ": trouble" << std::endl; will only be executed if the master's level is at least min_debug_level | |
debug_master | from where debug info will be taken |
delete_after_main
parameter unless the thread was created using new
. Since such threads may finish (and be destroyed) anytime, it makes no sense to refer to them after they have started. Thus a typical usage would be new Thread(true,..)->start();
virtual Dv::Thread::Thread::~Thread | ( | ) | [virtual] |
Destructor.
If this function is called by the thread itself, and it has not been joined nor detached, the thread will first detach itself. Otherwise, it will throw an exception.
std::logic_error | if the thread was neither joined nor detached unless the destructor was called by the thread itself, in which case an automatic detach will be done during the destruction. | |
std::logic_error | iff the executing thread is not this thread and this thread was not yet detached or joined. |
Reimplemented in Dv::Thread::Pool::Thread.
Dv::Thread::Thread::Thread | ( | const Thread & | ) | [private] |
Forbid cctor.
int Dv::Thread::Thread::start | ( | ) |
Start up thread, executing the main function of this Thread object.
Upon return, the new thread has actually already received some CPU time (actually, it may already have finished).
runtime_error | if thread is already running. |
Referenced by Dv::Thread::ActorPool< HandlerFactory >::Actor::Actor(), Dv::Thread::Actor< Handler >::Actor(), and Dv::Thread::ActorPool< HandlerFactory >::ActorPool().
virtual int Dv::Thread::Thread::main | ( | ) | [virtual] |
Main function of thread.
Reimplemented in Dv::Thread::Actor< Handler >, Dv::Thread::ActorPool< HandlerFactory >::Actor, Dv::Thread::ActorPool< HandlerFactory >, Dv::Thread::Pool::Thread, Reader, and Writer.
int Dv::Thread::Thread::join | ( | ) |
Wait for a non-detached thread to finish.
If the argument thread is not detached, it will be joined using pthread_join
. If the thread never started, the return value will be 0.
delete_after_main
function. Besides being automatically detached, such threads may have been deleted already at the time of the call to join, making the references to members in the code of Dv::Thread::Thread::join unsafe. MyThread t; cout << "starting t" << endl; t.start(); cout << "waiting for t to finish" << endl; join(t);
std::logic_error | if, by chance, the system correctly detects that the thread was detached and/or created using delete_after_main . |
void Dv::Thread::Thread::wait | ( | ) |
Wait for a thread to stop running.
This works even for detached threads or threads that have been deleted (e.g. threads that were created using the delete_after_main
option).
Reimplemented in Dv::Thread::Pool::Thread.
Referenced by Dv::Thread::ActorPool< HandlerFactory >::~ActorPool().
virtual void Dv::Thread::Thread::kill | ( | int | signal = SIGUSR1 |
) | [virtual] |
Kill a thread.
By default, this function simply sets killed_. If the thread is no longer running, this function has no effect. This function is 'delete-after-main' safe.
std::logic_error | iff a thread tries to kill itself |
Referenced by Dv::Thread::ActorPool< HandlerFactory >::~ActorPool().
int Dv::Thread::Thread::status | ( | ) | const [inline] |
Return exit status of this thread.
References exit_status_.
pthread_t Dv::Thread::Thread::id | ( | ) | const [inline] |
Return posix thread id of this thread.
References id_.
Referenced by Writer::main(), and Reader::main().
std::string Dv::Thread::Thread::sid | ( | ) | const |
String representation of thread id of this thread.
int Dv::Thread::Thread::detach | ( | ) | const |
Detach this thread.
Threads are created as ``joinable'', this function makes this impossible. It also ensures that all resources are freed when the thread exits.
Referenced by Dv::Thread::ActorPool< HandlerFactory >::main().
bool Dv::Thread::Thread::is_self | ( | ) | const |
Is this thread the currently running thread? This function is 'delete-after-main safe'.
bool Dv::Thread::Thread::running | ( | ) | const |
Is this thread running?
delete_after_main
, it may be that 'this' is no longer valid. However, this function should still work since it does not touch any members of Dv::Thread::Thread. static Thread* Dv::Thread::Thread::self | ( | ) | [static] |
Return pointer to currently running thread or 0.
static pthread_t Dv::Thread::Thread::self_id | ( | ) | throw () [inline, static] |
Return id (pthread_t
) of the currently running thread.
This works for any thread; it is actually just a call to pthread_self.
Referenced by Dv::Thread::MailBox< MessageReply >::put().
static void Dv::Thread::Thread::sleep | ( | size_t | millisecs | ) | [static] |
Safe version of sleep which uses a 'dummy' Monitor::wait.
pthread_t Dv::Thread::Thread::joined_by | ( | ) | const [inline] |
Has this thread been joined?
References joiner_.
pthread_t Dv::Thread::Thread::detached_by | ( | ) | const [inline] |
Has this thread been detached?
References detacher_.
static size_t Dv::Thread::Thread::wait_for_all | ( | ) | [static] |
Wait for all threads to have finished.
static int Dv::Thread::Thread::sig_set_handler | ( | int | signal = SIGUSR1 , |
|
void(*)(int) | handler = Dv::Thread::Thread::sig_handler , |
|||
int | sigaction_flags = 0 | |||
) | [static] |
Set signal handler for all threads (in this process).
The handling of OS signals in C++ (and multi-threaded applications) is problematic:
test-signal-exception.C
included with the distribution.The present implementation only provides a bare-bones minimal solution which seems to work reasonably well:
sig_atomic_t
killed_
associated with that thread to the signal number. Note that, by default, no handlers are set.test-default-sig-handler.C
for an example of an implementation of Dv::Thread::Thread::kill(int) that does deliver a signal.This implementation uses sigact.
signal | for which handler is set | |
handler | to call if signal s is received | |
sigaction_flags | passed as sa_mask in sigaction, see the man page for sigaction(2) . |
static void Dv::Thread::Thread::sig_handler | ( | int | signal | ) | [static] |
Default signal handler.
This handler will set the killed_ data member for the thread receiving the signal.
signal | used by pthread_kill |
static int Dv::Thread::Thread::sig_block | ( | int | signal, | |
bool | unblock = false | |||
) | [static] |
Block or unblock a signal for the currently running thread.
This implementation uses pthread_sigmask.
signal | to (un)block | |
unblock | if true, unblock instead of block the signal |
static int Dv::Thread::Thread::sig_unblock | ( | int | signal | ) | [static] |
Unblock a signal for the currently running thread.
This implementation uses pthread_sigmask.
signal | to unblock |
void Dv::Thread::Thread::exit | ( | int | status | ) | [protected] |
Finish this thread, with status.
This function is 'delete-after-main safe'.
status | to return. |
this==DvThread::Thread::self()
must hold try { exit(3); } catch (integer e) { }
The implementation of this function was changed between versions 0.4 and 0.5 because the previous implementation, which used pthread_exit, gave problems on systems with the nptl thread implementation.
std::logic_error | if the calling thread is not this thread |
int Dv::Thread::Thread::killed | ( | ) | const [inline, protected] |
References killed_.
Referenced by Dv::Thread::ActorPool< HandlerFactory >::main(), Dv::Thread::ActorPool< HandlerFactory >::Actor::main(), and Dv::Thread::Actor< Handler >::main().
bool Dv::Thread::Thread::delete_after_main | ( | ) | const [inline, private] |
Will this thread object be deleted after Thread::main() has completed?
References delete_after_main_.
bool Dv::Thread::Thread::at_end | ( | ) | const [inline, private] |
References at_end_.
static void* Dv::Thread::Thread::run_thread | ( | Dv::Thread::Thread * | t | ) | [static, private] |
Startup function wrapping around t->main, passed to pthread_create.
t | thread to start running |
void Dv::Thread::Thread::set_status | ( | int | status | ) | [inline, private] |
Set the exit status of this thread.
status | to set as exit status, normally result of main() |
References exit_status_.
friend class Threads [friend] |
Singleton class that keeps the relation between Dv::Thread::Thread objects and corresponding pthread_t
objects (i.e.
thread identifiers in the pthread
library).
int start | ( | Thread & | t | ) | [friend] |
volatile sig_atomic_t Dv::Thread::Thread::killed_ [protected] |
Set by default signal handler.
Referenced by killed().
pthread_t Dv::Thread::Thread::id_ [private] |
Posix thread id.
Referenced by id().
pthread_attr_t Dv::Thread::Thread::attributes_ [private] |
Posix thread attributes.
int Dv::Thread::Thread::exit_status_ [private] |
Exit status of this thread.
Referenced by set_status(), and status().
bool Dv::Thread::Thread::delete_after_main_ [private] |
Should this object be deleted after Thread::main has completed?
Referenced by delete_after_main().
pthread_t Dv::Thread::Thread::joiner_ [private] |
pthread_t id of thread that has joined this thread or 0
Referenced by joined_by().
pthread_t Dv::Thread::Thread::detacher_ [private] |
pthread_t id of thread that has detached this thread or 0
Referenced by detached_by().
int Dv::Thread::Thread::cancel_state_ [private] |
we disable this
Barrier Dv::Thread::Thread::barrier_ [private] |
private barrier, used to synchronize parent and new thread
bool Dv::Thread::Thread::at_end_ [private] |
set at end of Dv::Thread::Thread::run_thread
Referenced by at_end().
dvthread-0.13.4 | [11 December, 2009] |