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. Internally, the thread pool handle has to be created first, then the tasks are inserted into this handle 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 uint32_t (*CallbackT) (void* invocation);
5
6 class Callable
7 {
8 public:
9 CallbackT fn;
10 void* cookie;
11
12 Callable(CallbackT p_fn, void* p_cookie);
13 void run();
14
15 };
16
17 class Poolable: public Wakeable
18 {
19 public:
20 uint32_t executionTimeLimit;
21 Callable callable;
22 virtual void wake(int amt,void* cookie=NULL);
23 };
24
25 }