Differences between revisions 3 and 6 (spanning 3 versions)
Revision 3 as of 2014-06-03 06:42:01
Size: 1211
Editor: HungTa
Comment:
Revision 6 as of 2014-06-03 06:55:43
Size: 1484
Editor: HungTa
Comment:
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:

{{{#!highlight cpp
namespace SAFplus
{
Line 18: Line 22:

{{{#!highlight cpp
Line 24: Line 26:
    run(CallbackT* fn)
    CallbackT fn;
    void* cookie;
    
    Callable(CallbackT p_fn, void* p_cookie);
    void run();
Line 29: Line 34:
class Poolable: public Wakeable
  {
  public:
    uint32_t executionTimeLimit;
    Callable callable;
    virtual void wake(int amt,void* cookie=NULL);
  };

}

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.

#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 }

SAFplus: ThreadPool (last edited 2014-06-04 18:42:59 by AndrewStone)