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

FXThreadManager.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                  Primary Thread manager                                       *
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 FXTHREADMANAGER_H
00023 #define FXTHREADMANAGER_H
00024 
00025 #ifndef FXBASEOBJECT_H
00026 #include "FXBaseObject.h"
00027 #endif
00028 namespace FXEX {
00029 class FXRunnable;
00030 class FXFastMutex;
00031 
00032 /// An FXThreadList is a list of threads running in the current process
00033 typedef FXArray<FXRunnable*> FXThreadList;
00034 
00035 /**
00036  * A Thread Manager is required to be created in the main thread, for a few reasons:
00037  *
00038  * 1. It allows worker thread to call 'wait()' on themselves to do non-busy thread waiting.
00039  *    This then means that if we exit our main thread, the worker thread will be woken,
00040  *    thus allowing the main thread to exit.
00041  *
00042  * 2. It allows worker thread to communicate with the main thread using the 'signal()' method.
00043  *    Internally the signal() method uses a form of mainthread->handle(..), thus allowing
00044  *    worker threads to communication with the main thread using the standard FOX event
00045  *    handling technique.
00046  *
00047  *    Note, this means that worker threads cant do a mainthread->handle(..).  The reason for
00048  *    this is that this would require a change to FOX at the lowest level which would
00049  *    severly impact performance for single threaded applications.
00050  *
00051  * FXThreadManager is a singleton object, ie you should only create one of them.
00052  */
00053 class FXAPI FXThreadManager : public FXBaseObject {
00054   FXDECLARE(FXThreadManager)
00055 
00056 private:
00057   FXThreadHandle     mainthread;  // the main thread
00058   FXThreadList       threadlist;  // list of currently executing threads
00059   FXFastMutex       *mutex;       // control access to various resoures (non-recursive)
00060   FXint              last;        // last pointed-to thread
00061 
00062   static FXThreadManager* manager;  // thread manager instance
00063 
00064 protected:
00065   FXThreadManager();
00066 
00067 public:
00068   /// create a thread manager
00069   FXThreadManager(FXApp *a,FXObject *tgt=NULL,FXSelector sel=0);
00070 
00071   /// get the current thread manager instance
00072   static FXThreadManager* instance();
00073 
00074   /// add a thread to the list of managed threads
00075   void add(FXRunnable *r);
00076 
00077   /// remove an existing thread
00078   void remove(FXRunnable *r);
00079 
00080   /// return the number of active threads (not including the main thread)
00081   FXint size();
00082 
00083   /// return the next available thread
00084   FXRunnable* next();
00085 
00086   /// dtor
00087   virtual ~FXThreadManager();
00088   };
00089 
00090 } // namespace FXEX
00091 #endif // FXTHREADMANAGER_H