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

FXBarrier.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *               Thread Barrier                                                  *
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 FXBARRIER_H
00023 #define FXBARRIER_H
00024 
00025 #ifndef FXLOCKABLE_H
00026 #include "FXLockable.h"
00027 #endif
00028 #ifndef FXTHREADEDOBJECT_H
00029 #include "FXThreadedObject.h"
00030 #endif
00031 namespace FXEX {
00032 class FXCondition;
00033 
00034 /**
00035  * FXBarrier is object which can be used to synchronise the execution of
00036  * multiple threads, (say if it was a global variable).
00037  */
00038 class FXAPI FXBarrier : public FXLockable, public FXThreadedObject {
00039   FXDECLARE(FXBarrier)
00040 
00041 private:
00042   FXCondition *condition;   // the point of the barrier
00043   FXuint       reset;       // reset to origonal threshold value
00044 
00045 private:
00046   // dummy copy constructor and operator= to prevent copying
00047   FXBarrier(const FXBarrier&);
00048   FXBarrier& operator=(const FXBarrier&);
00049 
00050 public:
00051   /// create me a barrier :-)
00052   FXBarrier(FXuint threshold=0,FXbool resets=TRUE,FXObject *tgt=NULL,FXSelector sel=0);
00053 
00054   /**
00055    * A thread attempting to invoke the lock(), a Barrier will
00056    * be blocked until enough threads have invoked the lock(), at
00057    * which point the threshold is met, and the barrier is released.
00058    */
00059   void lock();
00060 
00061   /**
00062    * trylock()ing a barrier will test if the barrier is at its threshold
00063    * if you were to lock() then all barriers are released
00064    */
00065   FXbool trylock(FXuint ms);
00066 
00067   /// unlock all existing barriers
00068   void unlock();
00069 
00070   /**
00071    * set to new threshold - automatically unlocks any held locks
00072    * note: you _really should_ unlock all existing barriers, yourself first
00073    */
00074   void setThreshold(FXuint threshold,FXbool resets=TRUE,FXbool unlockAll=TRUE);
00075 
00076   // dtor
00077   ~FXBarrier();
00078   };
00079 
00080 } // namespace FXEX
00081 #endif // FXBARRIER_H