CVR-Lib last update 20 Sep 2009

cvrThread.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2005
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 /**
00042  * \file   cvrThread.h
00043  *         Contains the cvr::thread wrapper class to encapulate the differences
00044  *         between the POSIX threads in *ix systems and the Win32 concepts.
00045  * \author Thomas Rusert
00046  * \author Pablo Alvarado
00047  * \date   04.11.1999
00048  *
00049  * $Id: cvrThread.h,v 1.5 2005/04/20 16:02:17 doerfler Exp $
00050  */
00051 
00052 #ifndef _CVR_THREAD_H_
00053 #define _CVR_THREAD_H_
00054 
00055 #include "cvrMutex.h"
00056 #include "cvrSemaphore.h"
00057 #include "cvrMacroSymbols.h"
00058 
00059 #ifndef _CVR_WIN32
00060 #  include <pthread.h>
00061 #else
00062 #  include <winbase.h>
00063 #endif
00064 
00065 namespace cvr {
00066 
00067   /**
00068    * CVR-Lib thread class.
00069    *
00070    * Representiation of a system thread (currently a posix thread for Unix
00071    * systems, and WIN32 thread for windows systems)
00072    *
00073    * If you need a thread, just inherit from this class and reimplement the
00074    * method run():
00075    * \code
00076    * class myThread : public thread {
00077    * protected:
00078    *   void run() {
00079    *     // your thread's job here!
00080    *   }
00081    * };
00082    *
00083    * ...
00084    *
00085    * // somewhere else in your code:
00086    *
00087    * myThread aThread;
00088    * aThread.start(); // do your threads job...
00089    * // continue with other things (your thread runs in parallel...)
00090    * \endcode
00091    */
00092   class thread {
00093   public:
00094     /**
00095      * Default constructor
00096      */
00097     thread();
00098 
00099     /**
00100      * Destructor.
00101      *
00102      * If the thread is still executing while destroying the thread object,
00103      * the thread will be forced to stop. Use join() to wait for the thread
00104      * to terminate itself.
00105      */
00106     virtual ~thread();
00107 
00108     /**
00109      * Start thread.
00110      *
00111      * This method is called by the parent thread, to start in a
00112      * second thread the execution of the run() method, which is overloaded
00113      * in the derived classes.
00114      */
00115     virtual void start();
00116 
00117     /**
00118      * Stop thread execution.
00119      *
00120      * Forces the thread to stop executing.
00121      *
00122      * Overload this function to cleanup things that the run() method
00123      * could leave unfinished after thread cancellation.
00124      */
00125     virtual void stop();
00126 
00127     /**
00128      * Wait for thread termination.
00129      *
00130      * This has to be called by a thread different than the one
00131      * executing the run() method, in order to wait that the run()
00132      * method normally finishes.
00133      */
00134     virtual void join();
00135 
00136     /**
00137      * Check if thread is alive.
00138      *
00139      * Test if the thread that should execute the run() method is still alive.
00140      *
00141      * @return true if the thread is alive, or false if it was already
00142      * stopped or finished.
00143      */
00144     bool isAlive() const;
00145 
00146     /**
00147      * Check which thread is calling.
00148      *
00149      * Returns whether the thread that calls this method represents the
00150      * thread executing the run() method (i.e. if the method
00151      * is directly or indirectly called within the run() method).
00152      *
00153      * @return true if the thread executing the run() method is the one that
00154      *              called the current method, or false otherwise.
00155      */
00156     bool representsCalledThread() const;
00157 
00158   protected:
00159     /**
00160      * Thread execution method.
00161      *
00162      * The whole thread object is somehow just a representation for
00163      * this method, since the created thread will just call it, (which
00164      * means will execute the overloaded run() method of the derived
00165      * class).  The thread will also persist as long as this method
00166      * does, i.e. the thread will stop when the method finishes.
00167      */
00168     virtual void run() = 0;
00169 
00170     /**
00171      * Clean up the thread.
00172      *
00173      * Method to be called when finishing regulary or cancelling the thread;
00174      * something like a destructor for the run() method.
00175      *
00176      * The default behaviour, if not overloaded, is do nothing.
00177      */
00178     virtual void cleanUp() {};
00179 
00180   private:
00181     /**
00182      * The function to be called by the OS to clean up the thread.
00183      *
00184      * It will be called with the instance of the object as a parameter.
00185      * The static interface allows to use it as a C function.
00186      */
00187     static void clean(void* threadObject);
00188 
00189     /**
00190      * Flag to indicate if the thread is alive or not.
00191      */
00192     bool alive;
00193 
00194     /**
00195      * Mutex to protect the access to the multithreading functions.
00196      */
00197     static mutex startMutex;
00198 
00199     /**
00200      * Semaphore used to join called thread with the calling one
00201      */
00202     semaphore suspendSem;
00203 
00204 # ifndef _CVR_WIN32 // UNIX/LINUX
00205     /**
00206      * The real C function called as the POSIX thread standard
00207      * requires.  It just wrapps calling the method run() of the given
00208      * instance.
00209      */
00210     static void* execute(void* threadObject);
00211 
00212     /**
00213      * Handler of the created POSIX thread.
00214      */
00215     pthread_t theThread;
00216 # else          // WINDOWS
00217     /**
00218      * The real C function called as the Win32 thread interface
00219      * requires.  It just wrapps calling the method run() of the given
00220      * instance.
00221      */
00222     static void execute(void* threadObject);
00223 
00224     /**
00225      * Windows handle of the thread.
00226      */
00227     HANDLE theThread;
00228 
00229     /**
00230      * ID of the called thread
00231      */
00232     unsigned int calledThreadId;
00233 # endif
00234   };
00235 
00236 }
00237 
00238 #endif
00239 

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8