On this page
std/threadpool
Source EditImplements Nim's parallel & spawn statements.
Unstable API.
See also
- threads module for basic thread support
- locks module for locks and condition variables
- asyncdispatch module for asynchronous IO
Imports
Types
-
FlowVarBase = ref FlowVarBaseObj
- Untyped base class for FlowVar[T]. Source Edit
Consts
Procs
-
proc awaitAndThen[T](fv: FlowVar[T]; action: proc (x: T) {.closure.})
-
Blocks until
fv
is available and then passes its value toaction
.Note that due to Nim's parameter passing semantics, this means that
Source EditT
doesn't need to be copied, soawaitAndThen
can sometimes be more efficient than the ^ proc. -
proc blockUntilAny(flowVars: openArray[FlowVarBase]): int {....raises: [], tags: [], forbids: [].}
-
Awaits any of the given
flowVars
. Returns the index of oneflowVar
for which a value arrived.A
flowVar
only supports one call toblockUntilAny
at the same time. That means if youblockUntilAny([a,b])
andblockUntilAny([b,c])
the second call will only block untilc
. If there is noflowVar
left to be able to wait on, -1 is returned.Note: This results in non-deterministic behaviour and should be avoided.
Source Edit -
proc parallel(body: untyped) {.magic: "Parallel", ...raises: [], tags: [], forbids: [].}
-
A parallel section can be used to execute a block in parallel.
body
has to be in a DSL that is a particular subset of the language.Please refer to the manual for further information.
Source Edit -
proc pinnedSpawn(id: ThreadId; call: sink typed) {.magic: "Spawn", ...raises: [], tags: [], forbids: [].}
-
Always spawns a new task on the worker thread with
id
, so that thecall
is always executed on the thread.
Source Editcall
has to be a proc callp(...)
wherep
is gcsafe and has a return type that is eithervoid
or compatible withFlowVar[T]
. -
proc preferSpawn(): bool {....raises: [], tags: [], forbids: [].}
-
Use this proc to determine quickly if a
spawn
or a direct call is preferable.If it returns
Source Edittrue
, aspawn
may make sense. In general it is not necessary to call this directly; use the spawnX template instead. -
proc setMaxPoolSize(size: range[1 .. MaxThreadPoolSize]) {....raises: [], tags: [], forbids: [].}
- Sets the maximum thread pool size. The default value of this is MaxThreadPoolSize. Source Edit
-
proc spawn(call: sink typed) {.magic: "Spawn", ...raises: [], tags: [], forbids: [].}
-
Always spawns a new task, so that the
call
is never executed on the calling thread.
Source Editcall
has to be a proc callp(...)
wherep
is gcsafe and has a return type that is eithervoid
or compatible withFlowVar[T]
.
Templates
-
template spawnX(call)
-
Spawns a new task if a CPU core is ready, otherwise executes the call in the calling thread.
Usually, it is advised to use the spawn proc in order to not block the producer for an unknown amount of time.
Source Editcall
has to be a proc callp(...)
wherep
is gcsafe and has a return type that is either 'void' or compatible withFlowVar[T]
.
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/threadpool.html