Size: 1999
Comment:
|
Size: 2071
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 21: | Line 21: |
// Definition of user task typedef uint32_t (*CallbackT) (void* invocation); |
// Definition of user task typedef void (*CallbackT) (void* invocation); typedef uint32_t (*UserCallbackT) (void* invocation); |
Line 24: | Line 25: |
class Callable | class Callable |
Line 35: | Line 36: |
class Poolable: public Wakeable | class Poolable: public Wakeable |
Line 40: | Line 41: |
uint32_t m_executionTimeLimit; Callable* m_preIdleFn; Callable* m_onDeckFn; |
uint32_t m_executionTimeLimit; Callable* m_clb; |
Line 44: | Line 44: |
Poolable(Callable* preIdlFn, Callable* onDeckFn); | Poolable(Callable* clb); |
Line 48: | Line 48: |
class ThreadPool: public Poolable | class ThreadPool: public Poolable |
Line 61: | Line 61: |
UserCallbackT* m_preIdleFn; UserCallbackT* m_onDeckFn; |
|
Line 70: | Line 72: |
The Thread Pool
Thread Pools are useful when you need to limit the number of threads running in your application at the same time. There is a performance overhead associated with starting a new thread, and each thread is also allocated some memory for its stack etc.
Instead of starting a new thread for every task to execute concurrently, the task can be passed to a thread pool. As soon as the pool has any idle threads the task is assigned to one of them and executed.
Thread pools are often used in multi threaded servers. Each connection arriving at the server via the network is wrapped as a task and passed on to a thread pool. The threads in the thread pool will process the requests on the connections concurrently.
Implementation
The Thread Pool is a client library that is linked with every component that uses it.
Header
#include <clThreadPool.hxx>
APIs
1 namespace SAFplus
2 {
3 // Definition of user task
4 typedef void (*CallbackT) (void* invocation);
5 typedef uint32_t (*UserCallbackT) (void* invocation);
6
7 class Callable
8 {
9 public:
10 CallbackT m_fn;
11 void* m_cookie;
12
13 Callable(CallbackT fn, void* cookie);
14 void run();
15
16 };
17
18 class Poolable: public Wakeable
19 {
20 public:
21 struct timespec m_startTime;
22 struct timespec m_endTime;
23 uint32_t m_executionTimeLimit;
24 Callable* m_clb;
25
26 Poolable(Callable* clb);
27 virtual void wake(int amt,void* cookie=NULL);
28 };
29
30 class ThreadPool: public Poolable
31 {
32 protected:
33 void createTask();
34 void startNewTask();
35 void taskEntry();
36
37 public:
38 short m_minThread;
39 short m_maxThread;
40 short m_numIdleTasks;
41 short m_flags;
42 Mutex m_mutex;
43 UserCallbackT* m_preIdleFn;
44 UserCallbackT* m_onDeckFn;
45 ThreadCondition m_cond;
46 uint32_t m_pendingJobs;
47
48 ThreadPool(); /* Initialize pool; call createTask() */
49 void run(); /* run task */
50
51 ~ThreadPool(); /* Finalize pool */
52
53 };
54 }