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

/home/jeroen/FOX/fox/fox-1.7.33/include/FXThreadPool.h
00001 /********************************************************************************
00002 *                                                                               *
00003 *                             T h r e a d   P o o l                             *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2006,2012 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (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                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXTHREADPOOL_H
00022 #define FXTHREADPOOL_H
00023 
00024 #ifndef FXRUNNABLE_H
00025 #include "FXRunnable.h"
00026 #endif
00027 
00028 namespace FX {
00029 
00030 
00031 class FXWorker;
00032 
00033 
00035 typedef FXPtrQueueOf<FXRunnable> FXJobQueue;
00036 
00037 
00060 class FXAPI FXThreadPool : public FXRunnable {
00061 private:
00062   FXJobQueue      queue;        // Job queue
00063   FXMutex         mutex;        // Pool mutex
00064   FXCondition     pcond;        // Producer condition
00065   FXCondition     ccond;        // Consumer condition
00066   FXCondition     wcond;        // Wait condition
00067   FXTime          expire;       // Quit if no job within this time
00068   volatile FXuint maximum;      // Maximum number of workers
00069   volatile FXuint minimum;      // Minimum number of workers
00070   volatile FXuint started;      // Number of workers started
00071   volatile FXuint stopped;      // Number of workers stopped
00072   volatile FXuint pwaiting;     // Producers waiting
00073   volatile FXuint cwaiting;     // Consumers waiting
00074   volatile FXuint watching;     // Threads watching completion
00075   volatile FXbool runs;         // Thread pool is running
00076 private:
00077   FXWorker* startWorker();
00078 private:
00079   FXThreadPool(const FXThreadPool&);
00080   FXThreadPool &operator=(const FXThreadPool&);
00081 public:
00082 
00086   FXThreadPool(FXuint sz=256);
00087 
00089   FXbool setSize(FXuint sz);
00090 
00092   FXuint getSize() const;
00093 
00095   FXbool active() const;
00096 
00098   FXuint getWaitingThreads() const;
00099 
00101   FXuint getRunningThreads() const;
00102 
00104   void setMinimumThreads(FXuint n);
00105 
00107   FXuint getMinimumThreads() const;
00108 
00110   void setMaximumThreads(FXuint n);
00111 
00113   FXuint getMaximumThreads() const;
00114 
00116   void setExpiration(FXTime ns=forever);
00117 
00119   FXTime getExpiration() const;
00120 
00132   FXuint start(FXuint min=1,FXuint max=32,FXuint cnt=0);
00133 
00140   FXbool wait();
00141 
00147   void stop();
00148 
00160   FXbool execute(FXRunnable *job,FXTime blocking=forever);
00161 
00169   virtual FXint run();
00170 
00175   virtual ~FXThreadPool();
00176   };
00177 
00178 }
00179 
00180 #endif

Copyright © 1997-2011 Jeroen van der Zijp