ThreadPool.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/synch/ThreadPool.h#12 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #ifndef Fennel_ThreadPool_Included
00025 #define Fennel_ThreadPool_Included
00026 
00027 #include <deque>
00028 #include <vector>
00029 #include "fennel/synch/SynchMonitoredObject.h"
00030 #include "fennel/synch/Thread.h"
00031 
00032 FENNEL_BEGIN_NAMESPACE
00033 
00034 class PooledThread;
00035 class ThreadTracker;
00036 
00040 class FENNEL_SYNCH_EXPORT ThreadPoolBase : protected SynchMonitoredObject
00041 {
00042     friend class PooledThread;
00043     void runPooledThread();
00044 
00045 protected:
00046     enum State {
00047         STATE_STARTED,
00048         STATE_STOPPING,
00049         STATE_STOPPED
00050     };
00051 
00052     std::vector<PooledThread *> threads;
00053     State state;
00054     LocalCondition stoppingCondition;
00055     ThreadTracker *pThreadTracker;
00056 
00057     explicit ThreadPoolBase();
00058     virtual ~ThreadPoolBase();
00059     virtual bool isQueueEmpty() = 0;
00060     virtual void runOneTask(StrictMutexGuard &) = 0;
00061 
00062 public:
00068     void start(uint nThreads);
00069 
00075     void stop();
00076 
00082     void setThreadTracker(ThreadTracker &threadTracker);
00083 };
00084 
00094 template <class Task>
00095 class ThreadPool : public ThreadPoolBase
00096 {
00097     std::deque<Task> queue;
00098 
00099     virtual bool isQueueEmpty()
00100     {
00101         return queue.empty();
00102     }
00103 
00104     virtual void runOneTask(StrictMutexGuard &guard)
00105     {
00106         Task task = queue.front();
00107         queue.pop_front();
00108         guard.unlock();
00109         task.execute();
00110         guard.lock();
00111     }
00112 
00113 public:
00117     explicit ThreadPool()
00118     {
00119         pThreadTracker = NULL;
00120     }
00121 
00125     virtual ~ThreadPool()
00126     {
00127     }
00128 
00135     void submitTask(Task &task)
00136     {
00137         StrictMutexGuard guard(mutex);
00138         assert(state == STATE_STARTED);
00139         queue.push_back(task);
00140         condition.notify_one();
00141     }
00142 };
00143 
00144 FENNEL_END_NAMESPACE
00145 
00146 #endif
00147 
00148 // End ThreadPool.h

Generated on Mon Jun 22 04:00:20 2009 for Fennel by  doxygen 1.5.1