threadpool
Implements Nim's spawn.
See also:
Unstable API.
Imports
Types
FlowVarBase = ref FlowVarBaseObj
- Untyped base class for
FlowVar[T]. Source Edit FlowVar[T] {...}{.compilerProc.} = ref FlowVarObj[T]- A data flow variable. Source Edit
ThreadId = range[0 .. MaxDistinguishedThread - 1]
- Source Edit
Consts
MaxThreadPoolSize = 256
- Maximum size of the thread pool. 256 threads should be good enough for anybody ;-) Source Edit
MaxDistinguishedThread = 32
- Maximum number of "distinguished" threads. Source Edit
Procs
proc blockUntil(fv: var FlowVarBaseObj) {...}{.raises: [], tags: [].}-
Waits until the value for the
fvarrives.Usually it is not necessary to call this explicitly.
Source Edit proc awaitAndThen[T](fv: FlowVar[T]; action: proc (x: T) {...}{.closure.})-
Blocks until the
fvis available and then passes its value toaction.Note that due to Nim's parameter passing semantics this means that
Source EditTdoesn't need to be copied soawaitAndThencan sometimes be more efficient than ^ proc. proc unsafeRead[T](fv: FlowVar[ref T]): ptr T
- Blocks until the value is available and then returns this value. Source Edit
proc `^`[T](fv: FlowVar[T]): T
- Blocks until the value is available and then returns this value. Source Edit
proc blockUntilAny(flowVars: openArray[FlowVarBase]): int {...}{.raises: [], tags: [].}-
Awaits any of the given
flowVars. Returns the index of oneflowVarfor which a value arrived.A
flowVaronly supports one call toblockUntilAnyat the same time. That means if youblockUntilAny([a,b])andblockUntilAny([b,c])the second call will only block untilc. If there is noflowVarleft to be able to wait on, -1 is returned.Note: This results in non-deterministic behaviour and should be avoided.
Source Edit proc isReady(fv: FlowVarBase): bool {...}{.raises: [], tags: [].}-
Determines whether the specified
FlowVarBase's value is available.If
Source Edittrue, awaitingfvwill not block. proc setMinPoolSize(size: range[1 .. MaxThreadPoolSize]) {...}{.raises: [], tags: [].}- Sets the minimum thread pool size. The default value of this is 4. Source Edit
proc setMaxPoolSize(size: range[1 .. MaxThreadPoolSize]) {...}{.raises: [], tags: [].}- Sets the maximum thread pool size. The default value of this is
MaxThreadPoolSize(256). Source Edit proc preferSpawn(): bool {...}{.raises: [], tags: [].}-
Use this proc to determine quickly if a
spawnor a direct call is preferable.If it returns
Source Edittrue, aspawnmay make sense. In general it is not necessary to call this directly; use spawnX template instead. proc spawn(call: sink typed): void {...}{.magic: "Spawn".}-
Always spawns a new task, so that the
callis never executed on the calling thread.
Source Editcallhas to be proc callp(...)wherepis gcsafe and has a return type that is eithervoidor compatible withFlowVar[T]. proc pinnedSpawn(id: ThreadId; call: sink typed): void {...}{.magic: "Spawn".}-
Always spawns a new task on the worker thread with
id, so that thecallis always executed on the thread.
Source Editcallhas to be proc callp(...)wherepis gcsafe and has a return type that is eithervoidor compatible withFlowVar[T]. proc parallel(body: untyped) {...}{.magic: "Parallel".}-
A parallel section can be used to execute a block in parallel.
bodyhas to be in a DSL that is a particular subset of the language.Please refer to the manual for further information.
Source Edit proc sync() {...}{.raises: [], tags: [TimeEffect].}-
A simple barrier to wait for all
spawn'ed tasks.If you need more elaborate waiting, you have to use an explicit barrier.
Source Edit
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 spawn proc in order to not block the producer for an unknown amount of time.
Source Editcallhas to be proc callp(...)wherepis gcsafe and has a return type that is either 'void' or compatible withFlowVar[T].
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/threadpool.html