Module actors

Actor support for Nim. An actor is implemented as a thread with a channel as its inbox. This module requires the --threads:on command line switch.

Example:

var
  a: ActorPool[int, void]
createActorPool(a)
for i in 0 .. < 300:
  a.spawn(i, proc (x: int) {.thread.} = echo x)
a.join()

Note: This whole module is deprecated. Use threadpool and spawn instead.

Types

Task[In, Out] = object {.
pure, final
.} when true: receiver action*: proc (x: In): Out {.
thread
.} ## action to execute; ## sometimes useful shutDown*: bool ## set to tell an actor to shut-down data*: In ## the data to process
a task   Source Edit
PActor[In, Out] = ptr Actor[In, Out]
an actor   Source Edit
ActorPool[In, Out] = object {.
pure, final
.} actors: seq[PActor[In, Out]] when true: outputs
an actor pool   Source Edit

Procs

proc spawn[In, Out](action: proc (self: PActor[In, Out]) {.
thread
.}): PActor[In, Out]
creates an actor; that is a thread with an inbox. The caller MUST call join because that also frees the actor's associated resources.   Source Edit
proc inbox[In, Out](self: PActor[In, Out]): ptr Channel[In]
gets a pointer to the associated inbox of the actor self.   Source Edit
proc running[In, Out](a: PActor[In, Out]): bool
returns true if the actor a is running.   Source Edit
proc ready[In, Out](a: PActor[In, Out]): bool
returns true if the actor a is ready to process new messages.   Source Edit
proc join[In, Out](a: PActor[In, Out])
joins an actor.   Source Edit
proc recv[In, Out](a: PActor[In, Out]): Task[In, Out]
receives a task from a's inbox.   Source Edit
proc send[In, Out, X, Y](receiver: PActor[In, Out]; msg: In; sender: PActor[X, Y])
sends a message to a's inbox.   Source Edit
proc send[In, Out](receiver: PActor[In, Out]; msg: In; sender: ptr Channel[Out] = nil)
sends a message to receiver's inbox.   Source Edit
proc sendShutdown[In, Out](receiver: PActor[In, Out])
send a shutdown message to receiver.   Source Edit
proc reply[In, Out](t: Task[In, Out]; m: Out)
sends a message to io's output message box.   Source Edit
proc `^`[T](f: ptr Channel[T]): T
alias for 'recv'.   Source Edit
proc createActorPool[In, Out](a: var ActorPool[In, Out]; poolSize = 4)
creates an actor pool.   Source Edit
proc sync[In, Out](a: var ActorPool[In, Out]; polling = 50)
waits for every actor of a to finish with its work. Currently this is implemented as polling every polling ms and has a slight chance of failing since we check for every actor to be in ready state and not for messages still in ether. This will change in a later version, however.   Source Edit
proc terminate[In, Out](a: var ActorPool[In, Out])
terminates each actor in the actor pool a and frees the resources attached to a.   Source Edit
proc join[In, Out](a: var ActorPool[In, Out])
short-cut for sync and then terminate.   Source Edit
proc spawn[In, Out](p: var ActorPool[In, Out]; input: In;
                  action: proc (input: In): Out {.
thread
.}): ptr Channel[Out]
uses the actor pool to run action(input) concurrently. spawn is guaranteed to not block.   Source Edit
proc spawn[In](p: var ActorPool[In, void]; input: In;
              action: proc (input: In) {.
thread
.})
uses the actor pool to run action(input) concurrently. spawn is guaranteed to not block.   Source Edit