libcaf  0.13.2
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
caf::actor_pool Class Reference

An actor poool is a lightweight abstraction for a set of workers. More...

#include <actor_pool.hpp>

Inheritance diagram for caf::actor_pool:
caf::abstract_actor caf::abstract_channel caf::ref_counted caf::memory_managed

Classes

class  broadcast
 Default policy class implementing broadcast dispatching. More...
 
class  random
 Default policy class implementing random dispatching. More...
 
class  round_robin
 Default policy class implementing simple round robin dispatching. More...
 

Public Types

using uplock = upgrade_lock< detail::shared_spinlock >
 
using actor_vec = std::vector< actor >
 
using factory = std::function< actor()>
 
using policy = std::function< void(uplock &, const actor_vec &, mailbox_element_ptr &, execution_unit *)>
 

Public Member Functions

void enqueue (const actor_addr &sender, message_id mid, message content, execution_unit *host) override
 Enqueues a new message to the channel.
 
void enqueue (mailbox_element_ptr what, execution_unit *host) override
 Enqueues a new message wrapped in a mailbox_element to the channel. More...
 
- Public Member Functions inherited from caf::abstract_actor
void attach (attachable_ptr ptr)
 Attaches ptr to this actor. More...
 
template<class F >
void attach_functor (F f)
 Convenience function that attaches the functor f to this actor. More...
 
actor_addr address () const
 Returns the logical actor address.
 
size_t detach (const attachable::token &what)
 Detaches the first attached object that matches what.
 
void link_to (const actor_addr &whom)
 Links this actor to whom.
 
template<class ActorHandle >
void link_to (const ActorHandle &whom)
 Links this actor to whom.
 
void unlink_from (const actor_addr &other)
 Unlinks this actor from whom.
 
template<class ActorHandle >
void unlink_from (const ActorHandle &other)
 Unlinks this actor from whom.
 
bool establish_backlink (const actor_addr &other)
 Establishes a link relation between this actor and other and returns whether the operation succeeded. More...
 
bool remove_backlink (const actor_addr &other)
 Removes the link relation between this actor and other and returns whether the operation succeeded. More...
 
uint32_t id () const
 Returns the unique ID of this actor.
 
uint32_t exit_reason () const
 Returns the actor's exit reason or exit_reason::not_exited if it's still alive. More...
 
virtual std::set< std::string > message_types () const
 Returns the set of accepted messages types as strings or an empty set if this actor is untyped. More...
 
execution_unithost () const
 Returns the execution unit currently used by this actor. More...
 
void host (execution_unit *new_host)
 Sets the execution unit for this actor.
 
- Public Member Functions inherited from caf::abstract_channel
node_id node () const
 Returns the ID of the node this actor is running on.
 
bool is_remote () const
 Returns true if node_ptr returns.
 
bool is_abstract_actor () const
 
bool is_abstract_group () const
 
- Public Member Functions inherited from caf::ref_counted
 ref_counted (const ref_counted &)
 
ref_countedoperator= (const ref_counted &)
 
void ref () noexcept
 Increases reference count by one.
 
void deref () noexcept
 Decreases reference count by one and calls request_deletion when it drops to zero. More...
 
bool unique () const noexcept
 Queries whether there is exactly one reference.
 
size_t get_reference_count () const noexcept
 
- Public Member Functions inherited from caf::memory_managed
virtual void request_deletion (bool decremented_rc) noexcept
 Default implementations calls `delete this, but can be overriden in case deletion depends on some condition or the class doesn't use default new/delete. More...
 

Static Public Member Functions

template<class T , class Join , class Split = detail::nop_split>
static policy split_join (Join jf, Split sf=Split(), T init=T())
 Default policy class implementing broadcast dispatching (split step) followed by a join operation F combining all individual results to a single result of type T. More...
 
static actor make (policy pol)
 Returns an actor pool without workers using the dispatch policy pol.
 
static actor make (size_t n, factory fac, policy pol)
 Returns an actor pool with n workers created by the factory function fac using the dispatch policy pol. More...
 

Additional Inherited Members

- Static Public Attributes inherited from caf::abstract_channel
static constexpr int is_abstract_actor_flag = 0x100000
 
static constexpr int is_abstract_group_flag = 0x200000
 
- Protected Member Functions inherited from caf::abstract_actor
 abstract_actor ()
 Creates a non-proxy instance.
 
 abstract_actor (actor_id aid, node_id nid)
 Creates a proxy instance for a proxy running on nid.
 
void cleanup (uint32_t reason)
 Called by the runtime system to perform cleanup actions for this actor. More...
 
bool exited () const
 Returns exit_reason() != exit_reason::not_exited.
 
- Protected Member Functions inherited from caf::abstract_channel
int flags () const
 
void flags (int new_value)
 
- Protected Attributes inherited from caf::ref_counted
std::atomic< size_t > rc_
 

Detailed Description

An actor poool is a lightweight abstraction for a set of workers.

The pool itself is an actor, meaning that it can be passed around in an actor system to hide the actual set of workers.

After construction, new workers can be added via `{'SYS', 'PUT', actor} messages, e.g.,send(my_pool, sys_atom::value, put_atom::value, worker). {'SYS', 'DELETE', actor}messages remove a worker from the set, whereas{'SYS', 'GET'}returns avector<actor>` containing all workers.

Note that the pool always sends exit messages to all of its workers when forced to quit. The pool monitors all of its workers. Messages queued up in a worker's mailbox are lost, i.e., the pool itself does not buffer and resend messages. Advanced caching or resend strategies can be implemented in a policy.

It is wort mentioning that the pool is not an event-based actor. Neither does it live in its own thread. Messages are dispatched immediately during the enqueue operation. Any user-defined policy thus has to dispatch messages with as little overhead as possible, because the dispatching runs in the context of the sender.

Member Function Documentation

void caf::actor_pool::enqueue ( mailbox_element_ptr  what,
execution_unit host 
)
overridevirtual

Enqueues a new message wrapped in a mailbox_element to the channel.

This variant is used by actors whenever it is possible to allocate mailbox element and message on the same memory block and is thus more efficient. Non-actors use the default implementation which simply calls the pure virtual version.

Reimplemented from caf::abstract_channel.

static actor caf::actor_pool::make ( size_t  n,
factory  fac,
policy  pol 
)
static

Returns an actor pool with n workers created by the factory function fac using the dispatch policy pol.

template<class T , class Join , class Split = detail::nop_split>
static policy caf::actor_pool::split_join ( Join  jf,
Split  sf = Split(),
init = T() 
)
static

Default policy class implementing broadcast dispatching (split step) followed by a join operation F combining all individual results to a single result of type T.

Template Parameters
TResult type received by the original sender.
JoinFunctor with signature void (T&, message&).
SplitFunctor with signature void (vector<pair<actor, message>>&, message&).

The documentation for this class was generated from the following file: