Can we declare states as fields in a class-based actor to get the same effects? Or is there some special handling in stateful_actor (e.g. access to a security thread)?
CAF runtime assumes actors must be isolated, i.e. only the actor himself is allowed access to his state. Therefore, access to the state of an actor is never synchronized. Interactive communication uses messaging, thereby avoiding race conditions in design.
The subjects mentioned allow programmers to write fewer classes (and therefore less boilerplate code), but there is also one notable difference between ClassCounter and StatefulCounter : the lifetime of variables. In ClassCounter , member variable i lives until the ClassCounter destructor is ClassCounter . Since the actors are counted, the destructor works if there is no link to it, i.e. There is no actor or actor_addr handle for the actor. In a StatefulCounter a CounterState member is created if an actor is initialized and destroyed if it terminates.
A condition exists only as long as the actor is alive. This is especially useful for interrupting loops. If you have two members A and B , you have a loop if A contains a reference to B through a member variable and vice versa. Instead, this subject disrupts this process instead. A automatically issues its link to B when exiting and vice versa.
source share