Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members

FXNullThread.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                   Simulated worker thread object                              *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2003 by Mathew Robertson.   All Rights Reserved.                *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************/
00022 #ifndef FXNULLTHREAD_H
00023 #define FXNULLTHREAD_H
00024 
00025 #ifndef FXRUNNABLE_H
00026 #include "FXRunnable.h"
00027 #endif
00028 namespace FXEX {
00029 
00030 /**
00031  * A null thread implements the thread object API, without using OS threads.
00032  * The purpose is to allow the developer to develop using a thread-like environment
00033  * without having to worry about concurrency issues.
00034  *
00035  * It also provides a way for threads to be debugged...
00036  */
00037 class FXAPI FXNullThread : public FXRunnable {
00038   FXDECLARE_ABSTRACT(FXNullThread)
00039 
00040 private:
00041   FXbool running;
00042   FXint  idleCount;
00043   FXuint idle;
00044 
00045 protected:
00046   FXint stopThread;
00047 
00048 public:
00049   enum {
00050     ID_RUN=FXRunnable::ID_LAST,
00051     ID_YIELD,
00052     ID_LAST
00053     };
00054 
00055 public:
00056   long onThreadRun(FXObject*,FXSelector,void*);
00057   long onThreadYield(FXObject*,FXSelector,void*);
00058 
00059 protected:
00060   /// This is the ctor you need when you sub-class FXThread
00061   FXNullThread(FXObject *tgt=NULL,FXSelector sel=0);
00062 
00063   /// sleep
00064   void sleep(FXint ms=0);
00065 
00066   /// yield the cpu
00067   void yield();
00068 
00069 public:
00070   /// start running the object - ptr is some data you may want to pass
00071   void start();
00072 
00073   /// you are meant to overload this in your child thread classes
00074   /// ...if you have one...
00075   virtual void run()=0;
00076 
00077   // stop a thread
00078   void stop() { stopThread++; }
00079 
00080   // is the thread running
00081   FXbool isRunning();
00082 
00083   // reset the thread
00084   void reset() { stopThread=0; }
00085 
00086   /// dtor
00087   virtual ~FXNullThread();
00088   };
00089 
00090 } // namespace FXEX
00091 #endif // FXNULLTHREAD_H