#include <thread.h>
Public Types | |
enum | errors { BAD_EXCEPTION = -1, DEFAULT_MAIN = -2 } |
Public Member Functions | |
Thread (bool delete_after_main=false) throw (std::runtime_error) | |
Constructor. | |
virtual | ~Thread () |
Destructor. | |
int | start () throw (std::runtime_error) |
Start up thread. | |
virtual int | main () |
Main function of thread. | |
virtual void | killed () throw () |
This function is called by the thread as a response to a SIGFPE signal being delivered from another thread, using Thread::kill. | |
int | join () const throw () |
Wait for this thread to finish. | |
int | kill () const throw (std::runtime_error) |
Kill a thread. | |
int | status () const |
Return exit status of this thread. | |
void | set_status (int status) |
pthread_t | id () const |
Return posix thread id of this thread. | |
std::string | sid () const |
String representation of thread id of this thread. | |
Thread & | detach () |
Detach this thread. | |
Thread & | delete_after_main (bool yes) |
Delete this thread object after Thread::main() has completed. | |
bool | delete_after_main () |
Will this thread object be deleted after Thread::main() has completed? | |
bool | is_self () const |
Is this thread the currently running thread? | |
bool | running () const |
Is this thread running? | |
void | set_signal_handler_installed () |
Set signal_handler_installed. | |
Static Public Member Functions | |
static Thread * | self () throw (std::runtime_error) |
Return pointer to currently running thread. | |
static pthread_t | self_id () throw () |
Return id (pthread_t) of currently running thread. | |
static void | exit (int status) |
Finish this thread, with status. | |
Static Public Attributes | |
static std::ostream * | log |
Stream to log info on, if not 0. | |
Private Member Functions | |
Thread (const Thread &) | |
Forbid cctor. | |
Thread & | operator= (const Thread &) |
Forbid assignment. | |
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? | |
bool | signal_handler_installed |
Is a signal handler for SIGFPE installed? This should really be private but it is not since it is set from a C function. |
Example Thread class definitions:
#ifndef DV_THREAD_READER_H #define DV_THREAD_READER_H #include <unistd.h> #include <dvthread/thread.h> #include "buffer.h" // Reader thread class. A reader simply retrieves items from a buffer. class Reader: public Dv::Thread::Thread { public: // Constructor; n is the number of items to retrieve. Reader(Buffer& buf, unsigned int n): Thread(), buffer_(buf), n_(n) {} // The function executed by this tread. virtual int main() { try { std::cerr << "reader main, id = " << id() << std::endl; for (unsigned int i=0;(i<n_);++i) buffer_.get(); } catch (std::exception& e) { std::cerr << "reader exception: " << e.what() << std::endl; return 1; } return 0; } // Destructor. Wait for this thread to finish before we detroy it. virtual ~Reader() { std::cerr << "~Reader" << std::endl; } private: // Buffer from where items will be retrieved. Buffer& buffer_; // Number of items to retrieve. unsigned int n_; }; #endif
#ifndef DV_THREAD_WRITER_H #define DV_THREAD_WRITER_H // $Id: writer.h,v 1.6 2006/06/24 09:39:28 dvermeir Exp $ #include <dvthread/thread.h> #include <unistd.h> #include "buffer.h" // A Thread class that writes 100 items to a buffer. class Writer: public Dv::Thread::Thread { public: // Constructor; n is the number of items to put. Writer(Buffer& buf, unsigned int n): Thread(), buffer_(buf), n_(n) {} // Main function of a Writer thread. int main() { std::cerr << "writer main, id = " << id() << std::endl; try { for (unsigned int i=0; (i<n_);++i) buffer_.put(i); } catch (std::exception& e) { std::cerr << e.what() << std::endl; return 1; } return 0; } // Destructor, join this thread first. virtual ~Writer() { std::cerr << "Writer::~Writer" << std::endl; } private: // Buffer to which items will be written. Buffer& buffer_; // Number of items to write. unsigned int n_; }; #endif
|
|
|
Constructor.
|
|
Destructor.
|
|
Forbid cctor.
|
|
Start up thread. This function will execute main().
|
|
Main function of thread.
|
|
This function is called by the thread as a response to a SIGFPE signal being delivered from another thread, using Thread::kill. This function is then called the internal handler for this signal.
|
|
Wait for this thread to finish.
|
|
Kill a thread.
This function delivers a
|
|
Return exit status of this thread.
|
|
|
|
Return posix thread id of this thread.
|
|
String representation of thread id of this thread.
|
|
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.
|
|
Delete this thread object after Thread::main() has completed. This feature is handy when there are an unknown number of threads that are created using new and the only thing to do after a thread has finished is to delete the Thread object. The code below shows a typical example. class MyThread: public Dv::Thread::Thread { public: MyThread* make(); // factory method to prohibit non-heap allocation. void main(); private: MyThread(...): Dv::Thread::Thread(true), .. { detach(); .. } }; while (accept) { MyThread::make()->start(); // will delete itself automatically }
|
|
Will this thread object be deleted after Thread::main() has completed?
|
|
Is this thread the currently running thread?
|
|
Is this thread running?
|
|
Return pointer to currently running thread.
|
|
Return id (pthread_t) of currently running thread. This works for any thread; it is actually just call to pthread_self.
|
|
Finish this thread, with status.
|
|
Forbid assignment.
|
|
Set signal_handler_installed. This function does nothing if not called by the thread itself.
|
|
Stream to log info on, if not 0.
|
|
Posix thread id.
|
|
Posix thread attributes.
|
|
Exit status of this thread.
|
|
Should this object be deleted after Thread::main has completed?
|
|
Is a signal handler for SIGFPE installed? This should really be private but it is not since it is set from a C function.
|
dvthread-0.5.0 | [22 June, 2006] |