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
PActor[In, Out] = ptr Actor[In, Out]
- an actor Source
ActorPool[In, Out] = object {.pure, final.} actors: seq[PActor[In, Out]] when true: outputs
- an actor pool Source
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
proc inbox[In, Out](self: PActor[In, Out]): ptr Channel[In]
- gets a pointer to the associated inbox of the actor self. Source
proc running[In, Out](a: PActor[In, Out]): bool
- returns true if the actor a is running. Source
proc ready[In, Out](a: PActor[In, Out]): bool
- returns true if the actor a is ready to process new messages. Source
proc join[In, Out](a: PActor[In, Out])
- joins an actor. Source
proc recv[In, Out](a: PActor[In, Out]): Task[In, Out]
- receives a task from a's inbox. Source
proc send[In, Out, X, Y](receiver: PActor[In, Out]; msg: In; sender: PActor[X, Y])
- sends a message to a's inbox. Source
proc send[In, Out](receiver: PActor[In, Out]; msg: In; sender: ptr Channel[Out] = nil)
- sends a message to receiver's inbox. Source
proc sendShutdown[In, Out](receiver: PActor[In, Out])
- send a shutdown message to receiver. Source
proc reply[In, Out](t: Task[In, Out]; m: Out)
- sends a message to io's output message box. Source
proc `^`[T](f: ptr Channel[T]): T
- alias for 'recv'. Source
proc createActorPool[In, Out](a: var ActorPool[In, Out]; poolSize = 4)
- creates an actor pool. Source
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
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
proc join[In, Out](a: var ActorPool[In, Out])
- short-cut for sync and then terminate. Source
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
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