CppZmqZoltanExt 0.0.1
Loading...
Searching...
No Matches
zmqzext::actor_t Class Reference

Class that implements the Actor pattern using ZMQ PAIR sockets. More...

#include <actor.h>

Public Member Functions

 actor_t (zmq::context_t &context)
 Constructs a new actor_t object.
 
 actor_t (actor_t const &)=delete
 Copy constructor is deleted.
 
actor_toperator= (actor_t const &)=delete
 Copy assignment operator is deleted.
 
 actor_t (actor_t &&other) noexcept
 Move constructor.
 
actor_toperator= (actor_t &&other) noexcept
 Move assignment operator.
 
 ~actor_t () noexcept
 Destroys the actor_t object.
 
void start (actor_fn_t func)
 Starts the actor thread with the provided function.
 
bool stop (std::chrono::milliseconds timeout=std::chrono::milliseconds{-1})
 Stops the actor thread.
 
zmq::socket_t & socket () noexcept
 Gets the parent socket for external communication.
 
bool is_started () const noexcept
 Checks if the actor thread was started.
 
bool is_stopped () const noexcept
 Checks if the actor thread was stopped.
 
void set_destructor_timeout (std::chrono::milliseconds timeout) noexcept
 Sets the timeout value used in the destructor.
 
std::chrono::milliseconds get_destructor_timeout () const noexcept
 Gets the current timeout value used in the destructor.
 

Detailed Description

Class that implements the Actor pattern using ZMQ PAIR sockets.

The Actor provides the functionality of running a user-provided function in a new thread and keeping a pair of zmq sockets for communication between the parent thread and the child thread. It synchronizes the start of the function execution in its start method and the stop of the thread in its stop method.

Definition at line 131 of file actor.h.

Constructor & Destructor Documentation

◆ actor_t() [1/3]

zmqzext::actor_t::actor_t ( zmq::context_t &  context)
explicit

Constructs a new actor_t object.

Creates a pair of zmq::sockets of type pair, one for the parent side and other for the child. The parent socket is bound to an automatic self-generated address that is unique for each instance. The child socket is connected to the parent's bound address.

Parameters
contextThe ZMQ context to be used for creating the sockets

Definition at line 49 of file actor.cpp.

◆ actor_t() [2/3]

zmqzext::actor_t::actor_t ( actor_t const &  )
delete

Copy constructor is deleted.

Actors cannot be copied because they maintain exclusive ownership of the communication sockets and represent a unique execution thread. Copying would violate the single ownership principle and lead to resource conflicts. Use move semantics instead to transfer actor ownership.

◆ actor_t() [3/3]

zmqzext::actor_t::actor_t ( actor_t &&  other)
noexcept

Move constructor.

Transfers ownership of the actor's communication sockets and execution state from the source actor to this newly constructed actor. The actor object can be moved before or after being started. If moved before start(), the new actor can be started normally. If called after start(), the new actor continues the execution of the original actor's thread. The source actor is left in a valid but empty state where is_started() and is_stopped() return true. Calling stop() on the moved-from actor is a no-op so it can be called in ins destructor.

Parameters
otherThe source actor to move from. After the move, other is in a valid but empty state.
Note
Move operations are noexcept and do not throw exceptions

Definition at line 59 of file actor.cpp.

◆ ~actor_t()

zmqzext::actor_t::~actor_t ( )
noexcept

Destroys the actor_t object.

Calls stop with the configurable destructor timeout to avoid blocking forever.

Definition at line 75 of file actor.cpp.

Member Function Documentation

◆ get_destructor_timeout()

std::chrono::milliseconds zmqzext::actor_t::get_destructor_timeout ( ) const
inlinenoexcept

Gets the current timeout value used in the destructor.

Returns
The current timeout value in milliseconds

Definition at line 271 of file actor.h.

◆ is_started()

bool zmqzext::actor_t::is_started ( ) const
inlinenoexcept

Checks if the actor thread was started.

Returns
true if started, false otherwise

Definition at line 252 of file actor.h.

◆ is_stopped()

bool zmqzext::actor_t::is_stopped ( ) const
inlinenoexcept

Checks if the actor thread was stopped.

Returns
true if stopped, false otherwise

Definition at line 259 of file actor.h.

◆ operator=() [1/2]

actor_t & zmqzext::actor_t::operator= ( actor_t &&  other)
noexcept

Move assignment operator.

Transfers ownership of the actor's communication sockets and execution state from the source actor to this actor. The actor object can be moved before or after being started. If moved before start(), the new actor can be started normally. If called after start(), the new actor continues the execution of the original actor's thread. The source actor is left in a valid but empty state where is_started() and is_stopped() return true. Calling stop() on the moved-from actor is a no-op so it can be called in ins destructor.

Parameters
otherThe source actor to move from. After the move, other is in a valid but empty state.
Returns
Reference to this actor
Note
Move operations are noexcept and do not throw exceptions

Definition at line 61 of file actor.cpp.

◆ operator=() [2/2]

actor_t & zmqzext::actor_t::operator= ( actor_t const &  )
delete

Copy assignment operator is deleted.

Actors cannot be copy-assigned because they maintain exclusive ownership of the communication sockets and represent a unique execution thread. Copying would violate the single ownership principle and lead to resource conflicts. Use move semantics instead to transfer actor ownership.

◆ set_destructor_timeout()

void zmqzext::actor_t::set_destructor_timeout ( std::chrono::milliseconds  timeout)
inlinenoexcept

Sets the timeout value used in the destructor.

Parameters
timeoutThe timeout value in milliseconds

Definition at line 265 of file actor.h.

◆ socket()

zmq::socket_t & zmqzext::actor_t::socket ( )
inlinenoexcept

Gets the parent socket for external communication.

Returns
zmq::socket_t& Reference to the parent socket

Definition at line 245 of file actor.h.

◆ start()

void zmqzext::actor_t::start ( actor_fn_t  func)

Starts the actor thread with the provided function.

Launches a new thread executing the provided function and blocks until receiving a success or failure signal. On success signal, returns normally. On failure signal, rethrows any saved exception from the execute method or throws std::runtime_error.

Parameters
funcThe function to be executed in the new thread. Must take a zmq::socket_t& and return bool
Exceptions
std::runtime_errorIf the thread was already started or if the function signals failure during function initialization
Rethrowsany exception caught during function initialization

Definition at line 82 of file actor.cpp.

◆ stop()

bool zmqzext::actor_t::stop ( std::chrono::milliseconds  timeout = std::chrono::milliseconds{-1})

Stops the actor thread.

Sends a stop request message and waits for a response signal using the provided timeout. If timeout is 0, returns immediately after trying to send the stop request. If timeout is negative, blocks indefinitely waiting for the response.

Parameters
timeout_msThe timeout in milliseconds. 0 for non-blocking, negative for infinite
Returns
true if successfully stopped, wasn't started or was already stopped, false if timed out

Definition at line 118 of file actor.cpp.


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