---
name: "ACE_TP_Reactor"
section: "3"
platform: "linux"
package: "libace-doc"
version: "5.0.7-4"
release: "Debian 2.2 (Potato)"
platforms: ["linux"]
url: "https://new.carta.tech/ACE_TP_Reactor"
---

# ACE\_TP\_Reactor(3)

Specialization of Select Reactor to support thread-pool based event dispatching.

## NAME

ACE\_TP\_Reactor -  Specialization of Select Reactor to support thread-pool based event dispatching.

## SYNOPSIS

```
#include <ace/TP_Reactor.h>
class ACE_TP_Reactor : public ACE_Select_Reactor
{
  public:
    ACE_TP_Reactor (
        ACE_Sig_Handler * = 0,
        ACE_Timer_Queue * = 0,
        int mask_signals = 1
        );
    ACE_TP_Reactor (
        size_t max_number_of_handles,
        int restart = 0,
        ACE_Sig_Handler * = 0,
        ACE_Timer_Queue * = 0,
        int mask_signals = 1
        );
    virtual int handle_events (ACE_Time_Value *max_wait_time = 0);
    virtual int handle_events (ACE_Time_Value &max_wait_time);
    virtual int mask_ops (
        ACE_Event_Handler *eh,
        ACE_Reactor_Mask mask,
        int ops
        );
    virtual int mask_ops (
        ACE_HANDLE handle,
        ACE_Reactor_Mask mask,
        int ops
        );
    static void no_op_sleep_hook (void *);
    virtual void wakeup_all_threads (void);
    ACE_ALLOC_HOOK_DECLARE;
  protected:
    virtual int dispatch_io_set (
        int number_of_active_handles,
        int& number_dispatched,
        int mask,
        ACE_Handle_Set& dispatch_mask,
        ACE_Handle_Set& ready_mask,
        ACE_EH_PTMF callback
        );
    virtual void notify_handle (
        ACE_HANDLE handle,
        ACE_Reactor_Mask mask,
        ACE_Handle_Set &,
        ACE_Event_Handler *eh,
        ACE_EH_PTMF callback
        );
    virtual int notify_handle (ACE_EH_Dispatch_Info &dispatch_info);
    ACE_EH_Dispatch_Info dispatch_info_;
  private:
    ACE_TP_Reactor (const ACE_TP_Reactor &);
    ACE_TP_Reactor &operator = (const ACE_TP_Reactor &);
};
```

## DESCRIPTION

TP\_Reactor (ala, Thread Pool Reactor) uses the leader-follower model to demultiplex requests among a bunch of threads. Basically, when using thread pool reactor, one will pre-spawn a \_fixed\_ number of threads.  When you issue the run\_event\_loop method, one of the thread will become the leader thread and wait for an event.  The other threads (followers) will be lined up and waiting for their turns to become the leader.  When an event occurs, the leader will pick a follower to become the leader and go on to handle the event.  The consequence of using TP\_Reactor is the amortization of the costs used to creating threads.  The context switching cost will also reduce.  More over, the total resources used by threads are bounded (because there are only so many threads.)

## Initialization and termination methods.

```
ACE_TP_Reactor (
    ACE_Sig_Handler * = 0,
    ACE_Timer_Queue * = 0,
    int mask_signals = 1
    );
```

Initialize ACE\_TP\_Reactor with the default size.

```
ACE_TP_Reactor (
    size_t max_number_of_handles,
    int restart = 0,
    ACE_Sig_Handler * = 0,
    ACE_Timer_Queue * = 0,
    int mask_signals = 1
    );
```

Initialize the ACE\_TP\_Reactor to manage max\_number\_of\_handles.  If restart is non-0 then the ACE\_Reactor's handle\_events method will be restarted automatically when EINTR occurs.  If signal\_handler or timer\_queue are non-0 they are used as the signal handler and timer queue, respectively.

## Event loop drivers.

```
virtual int handle_events (ACE_Time_Value *max_wait_time = 0);
```

This event loop driver that blocks for max\_wait\_time before returning.  It will return earlier if timer events, I/O events, or signal events occur.  Note that max\_wait\_time can be 0, in which case this method blocks indefinitely until events occur.

max\_wait\_time is decremented to reflect how much time this call took.  For instance, if a time value of 3 seconds is passed to handle\_events and an event occurs after 2 seconds, max\_wait\_time will equal 1 second.  This can be used if an application wishes to handle events for some fixed amount of time.

Returns the total number of ACE\_Event\_Handlers that were dispatched, 0 if the max\_wait\_time elapsed without dispatching any handlers, or -1 if something goes wrong.

```
virtual int handle_events (ACE_Time_Value &max_wait_time);
```

```
virtual int mask_ops (
    ACE_Event_Handler *eh,
    ACE_Reactor_Mask mask,
    int ops
    );
```

GET/SET/ADD/CLR the dispatch mask "bit" bound with the eh and mask.

```
virtual int mask_ops (
    ACE_HANDLE handle,
    ACE_Reactor_Mask mask,
    int ops
    );
```

GET/SET/ADD/CLR the dispatch mask "bit" bound with the handle and mask.

```
static void no_op_sleep_hook (void *);
```

Called from handle events

```
virtual void wakeup_all_threads (void);
```

Wake up all threads in waiting in the event loop

```
ACE_ALLOC_HOOK_DECLARE;
```

Declare the dynamic allocation hooks.

## Internal methods that do the actual work.

```
virtual int dispatch_io_set (
    int number_of_active_handles,
    int& number_dispatched,
    int mask,
    ACE_Handle_Set& dispatch_mask,
    ACE_Handle_Set& ready_mask,
    ACE_EH_PTMF callback
    );
```

Overwrites ACE\_Select\_Reactor::dispatch\_io\_set to \*not\* dispatch any event handlers.  The information of one activated event handler is stored away, so that the event handler can be dispatch later.

```
virtual void notify_handle (
    ACE_HANDLE handle,
    ACE_Reactor_Mask mask,
    ACE_Handle_Set &,
    ACE_Event_Handler *eh,
    ACE_EH_PTMF callback
    );
```

This method shouldn't get called.

```
virtual int notify_handle (ACE_EH_Dispatch_Info &dispatch_info);
```

Notify the appropriate callback in the context of the eh associated with handle that a particular event has occurred.

```
ACE_EH_Dispatch_Info dispatch_info_;
```

Dispatch information of the activated event handler

## AUTHOR

Irfan Pyarali Nanbor Wang

## LIBRARY

ace
