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

FXThreadPool.h
1 /********************************************************************************
2 * *
3 * T h r e a d P o o l *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2006,2022 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXTHREADPOOL_H
22 #define FXTHREADPOOL_H
23 
24 #ifndef FXRUNNABLE_H
25 #include "FXRunnable.h"
26 #endif
27 
28 namespace FX {
29 
30 
31 class FXWorker;
32 
34 typedef FXLFQueueOf<FXRunnable> FXTaskQueue;
35 
36 
68 class FXAPI FXThreadPool : public FXRunnable {
69 private:
70  FXTaskQueue queue; // Task queue
71  FXCompletion tasks; // Active tasks
72  FXCompletion threads; // Active threads
73  FXSemaphore freeslots; // Free slots in queue
74  FXSemaphore usedslots; // Used slots in queue
75  FXuval stacksize; // Stack size
76  FXTime expiration; // Quit if no task within this time
77  volatile FXuint maximum; // Maximum threads
78  volatile FXuint minimum; // Minimum threads
79  volatile FXuint workers; // Working threads
80  volatile FXuint running; // Context is running
81 private:
82  static FXAutoThreadStorageKey reference;
83 private:
84  FXbool startWorker();
85  void runWhile(FXCompletion& comp,FXTime timeout);
86  virtual FXint run();
87 private:
88  FXThreadPool(const FXThreadPool&);
89  FXThreadPool &operator=(const FXThreadPool&);
90 public:
91 
95  FXThreadPool(FXuint sz=256);
96 
98  FXbool active() const { return running==1; }
99 
101  FXbool setSize(FXuint sz);
102 
104  FXuint getSize() const { return queue.getSize(); }
105 
107  FXuint getRunningTasks() const { return tasks.count(); }
108 
110  FXuint getRunningThreads() const { return threads.count(); }
111 
113  FXbool setMinimumThreads(FXuint n);
114 
116  FXuint getMinimumThreads() const { return minimum; }
117 
119  FXbool setMaximumThreads(FXuint n);
120 
122  FXuint getMaximumThreads() const { return maximum; }
123 
125  FXbool setExpiration(FXTime ns=forever);
126 
128  FXTime getExpiration() const { return expiration; }
129 
131  FXbool setStackSize(FXuval sz);
132 
134  FXuval getStackSize() const { return stacksize; }
135 
137  static FXThreadPool* instance();
138 
140  static void instance(FXThreadPool *pool);
141 
149  FXuint start(FXuint count=0);
150 
159  FXbool execute(FXRunnable* task,FXTime blocking=forever);
160 
170  FXbool executeAndWait(FXRunnable* task,FXTime blocking=forever);
171 
181  FXbool executeAndWaitFor(FXRunnable* task,FXCompletion& comp,FXTime blocking=forever);
182 
189  FXbool wait();
190 
198  FXbool waitFor(FXCompletion& comp);
199 
208  FXbool stop();
209 
213  virtual ~FXThreadPool();
214  };
215 
216 
217 }
218 
219 #endif
FXuval getStackSize() const
Get stack size.
Definition: FXThreadPool.h:134
FXuint getSize() const
Return task queue size.
Definition: FXThreadPool.h:104
FXuint getRunningThreads() const
Return number of threads.
Definition: FXThreadPool.h:110
A semaphore allows for protection of a resource that can be accessed by a fixed number of simultaneou...
Definition: FXSemaphore.h:38
FXbool active() const
Return true if running.
Definition: FXThreadPool.h:98
FXuint getMaximumThreads() const
Return maximum number of worker threads.
Definition: FXThreadPool.h:122
FXuint getMinimumThreads() const
Return minimum number of worker threads.
Definition: FXThreadPool.h:116
FXuint getSize() const
Return size.
Definition: FXLFQueue.h:50
FXuint getRunningTasks() const
Return number of tasks.
Definition: FXThreadPool.h:107
Definition: FX4Splitter.h:28
FXTime getExpiration() const
Get expiration time.
Definition: FXThreadPool.h:128
A completion counter allows a single thread to monitor a number of ongoing concurrent activities for ...
Definition: FXCompletion.h:41
Automatically generated thread-local storage key.
Definition: FXAutoThreadStorageKey.h:40
FXuint count() const
Return current counter value.
Definition: FXCompletion.h:54
FXRunnable represents a generic runnable thing.
Definition: FXRunnable.h:33
A Thread Pool manages execution of tasks on a number of worker-threads.
Definition: FXThreadPool.h:68

Copyright © 1997-2022 Jeroen van der Zijp