|
CppZmqZoltanExt 0.0.1
|
Actor pattern implementation using ZeroMQ PAIR sockets. More...
#include <chrono>#include <functional>#include <memory>#include <mutex>#include <zmq.hpp>#include "cppzmqzoltanext/czze_export.h"

Go to the source code of this file.
Classes | |
| class | zmqzext::actor_t |
| Class that implements the Actor pattern using ZMQ PAIR sockets. More... | |
Typedefs | |
| using | zmqzext::actor_fn_t = std::function< bool(zmq::socket_t &)> |
| Alias for a function type used to define actor behaviors. | |
Actor pattern implementation using ZeroMQ PAIR sockets.
This module provides the actor_t class, which implements the Actor pattern for concurrent programming. An actor is an independent execution unit (running in its own thread) that runs a user-defined function and communicates with the parent through ZeroMQ PAIR sockets.
Thread Safety
The actor_t class is NOT thread-safe for most operations. The design aims to avoid memory sharing between the parent and child threads, except for the child socket passed to the user function. The actor_t object and its parent socket should only be accessed from the thread that created the actor. The child socket is passed to the user function and runs in the actor thread. Additional parameters can be passed to the user function via captures in lambdas or functors, but the lifetime of passed parameters must be managed carefully. Usually, parameters should be copied or moved into the user function to avoid dangling references. Pointers where the ownership is transferred to the user function are also acceptable.
Initialization Synchronization
The start() method blocks until the user function sends either a success or failure signal. This ensures initialization of the actor is synchronized with the calling thread.
Exception Handling
If an exception is thrown in the user function (other than zmq::error_t) before sending the success signal, it will be captured and re-thrown in the start() method, allowing the parent to handle initialization errors. After sending the success signal, its the user's responsibility to handle exceptions within the user function. Still, the actor_t class will catch unhandled exceptions and silently exit the thread to avoid crashing the application.
Finalization Synchronization
The user function finalization is requested by the stop() method which can be called explicitly or is implicitly by the destructor. The stop() method sends a stop request and waits for a response signal, with a configurable timeout. It is the user's responsibility to handle the stop request in the user function and exit immediately. Usually, the user function communicates to the parent application that it has finished its work (either by its own logic or by a previous request) and then the parent application destroys the actor, starting the stop synchronization.
Key features:
Distributed under the MIT License (MIT) (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)
Definition in file actor.h.
| using zmqzext::actor_fn_t = typedef std::function<bool(zmq::socket_t&)> |
Alias for a function type used to define actor behaviors.
This type represents a callable object (such as a lambda, function pointer, or functor) that takes a reference to a zmq::socket_t and returns a boolean value.
The actor should send a success signal through the provided socket as soon as it has completed its initialization successfully. If initialization fails, it may throw an exception, which will be rethrown in the parent thread, or just return false, which will throw a std::runtime_error in the parent thread.
After the actor sends the success signal, it should monitor for stop requests and return immediately when a stop request is received. The return value has no meaning after initialization.
| socket | Reference to a ZeroMQ socket used by the actor for communication. |