Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

pthreadpool.h

Go to the documentation of this file.
00001 /*
00002  *   P::Classes - Portable C++ Application Framework
00003  *   Copyright (C) 2000-2002  Christian Prochnow <cproch@seculogix.de>
00004  *
00005  *   This library is free software; you can redistribute it and/or
00006  *   modify it under the terms of the GNU Lesser General Public
00007  *   License as published by the Free Software Foundation; either
00008  *   version 2 of the License, or (at your option) any later version.
00009  *
00010  *   This library is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *   Lesser General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU Lesser General Public
00016  *   License along with this library; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef _pthreadpool_h_
00021 #define _pthreadpool_h_
00022 
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/pthread_.h>
00025 #include <pclasses/psemaphore.h>
00026 #include <pclasses/pcriticalsection.h>
00027 #include <pclasses/psignal.h>
00028 #include <queue>
00029 #include <list>
00030 
00031 namespace P {
00032 
00033 class ThreadPool;
00034 class WorkerThread;
00035 
00036 class CXX_CLASS_EXPORT ThreadJob {
00037   friend class WorkerThread;
00038 
00039   public:
00040     ThreadJob();
00041     virtual ~ThreadJob();
00042 
00043     void wait();
00044     void tryWait();
00045 
00046     Signal0<int> sigMain;
00047 
00048   protected:
00049     virtual void main();
00050     void finished();
00051 
00052   private:
00053     Semaphore   m_lock;
00054 
00055 };
00056 
00057 class CXX_CLASS_EXPORT ThreadPool {
00058   friend class WorkerThread;
00059 
00060   public:
00061     ThreadPool(unsigned int numThreadsInit, unsigned int numThreadsMaxIdle,
00062                unsigned int numThreadsMax);
00063     ~ThreadPool();
00064 
00065     void enqueue(ThreadJob* job);
00066 
00067   protected:
00068     void finished(WorkerThread* worker);
00069 
00070   private:
00071     CriticalSection   m_lock;
00072     Semaphore         m_threadStartSem;
00073     unsigned int      m_numThreads;
00074     unsigned int      m_numThreadsMaxIdle;
00075     unsigned int      m_numThreadsMax;
00076     unsigned int      m_numThreadsIdle;
00077     std::queue<
00078       ThreadJob*>     m_jobs;
00079     std::queue<
00080       WorkerThread*>  m_idle;
00081 
00082 };
00083 
00084 }
00085 
00086 #endif

Generated on Fri Mar 12 21:08:31 2004 for P::Classes by doxygen 1.3.3