# threadpool

- マニュアル
- 標準ライブラリ
- 索引

検索
分類:

Implements Nim's spawn.

関連:

- threads module
- channels module
- locks module
- asyncdispatch module

この API は不安定です。

# インポート

cpuinfo, cpuload, locks, os

# 型

    FlowVarBase = ref FlowVarBaseObj

  Untyped base class for `FlowVar[T]`. ソース 編集

    FlowVar[T] {...}{.compilerProc.} = ref FlowVarObj[T]

  A data flow variable. ソース 編集

    ThreadId = range[0 .. 32 - 1]

  ソース 編集

# 定数

    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. ソース 編集

# プロシージャ

    proc blockUntil(fv: FlowVarBase) {...}{.raises: [], tags: [].}

  Waits until the value for the `fv` arrives.

  Usually it is not necessary to call this explicitly.

  ソース 編集

    proc awaitAndThen[T](fv: FlowVar[T]; action: proc (x: T) {...}{.closure.})

  Blocks until the `fv` is available and then passes its value to `action`.

  Note that due to Nim's parameter passing semantics this means that `T` doesn't need to be copied so `awaitAndThen` can 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. ソース 編集

    proc `^`[T](fv: FlowVar[ref T]): ref T

  Blocks until the value is available and then returns this value. ソース 編集

    proc `^`[T](fv: FlowVar[T]): T

  Blocks until the value is available and then returns this value. ソース 編集

    proc blockUntilAny(flowVars: openArray[FlowVarBase]): int {...}{.raises: [], tags: [].}

  Awaits any of the given `flowVars`. Returns the index of one `flowVar` for which a value arrived.

  A `flowVar` only supports one call to `blockUntilAny` at the same time. That means if you `blockUntilAny([a,b])` and `blockUntilAny([b,c])` the second call will only block until `c`. If there is no `flowVar` left to be able to wait on, -1 is returned.

  Note: This results in non-deterministic behaviour and should be avoided.

  ソース 編集

    proc isReady(fv: FlowVarBase): bool {...}{.raises: [], tags: [].}

  Determines whether the specified `FlowVarBase`'s value is available.

  If `true`, awaiting `fv` will not block.

  ソース 編集

    proc setMinPoolSize(size: range[1 .. MaxThreadPoolSize]) {...}{.raises: [], tags: [].}

  Sets the minimum thread pool size. The default value of this is 4. ソース 編集

    proc setMaxPoolSize(size: range[1 .. MaxThreadPoolSize]) {...}{.raises: [], tags: [].}

  Sets the maximum thread pool size. The default value of this is `MaxThreadPoolSize` (256). ソース 編集

    proc preferSpawn(): bool {...}{.raises: [], tags: [].}

  Use this proc to determine quickly if a `spawn` or a direct call is preferable.

  If it returns `true`, a `spawn` may make sense. In general it is not necessary to call this directly; use spawnX template instead.

  ソース 編集

    proc spawn(call: typed): void {...}{.magic: "Spawn".}

  Always spawns a new task, so that the `call` is never executed on the calling thread.

  `call` has to be proc call `p(...)` where `p` is gcsafe and has a return type that is either `void` or compatible with `FlowVar[T]`.

  ソース 編集

    proc pinnedSpawn(id: ThreadId; call: typed): void {...}{.magic: "Spawn".}

  Always spawns a new task on the worker thread with `id`, so that the `call` is always executed on the thread.

  `call` has to be proc call `p(...)` where `p` is gcsafe and has a return type that is either `void` or compatible with `FlowVar[T]`.

  ソース 編集

    proc parallel(body: untyped) {...}{.magic: "Parallel".}

  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.

  ソース 編集

    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.

  ソース 編集

# テンプレート

    template spawnX(call): void

  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.

  `call` has to be proc call `p(...)` where `p` is gcsafe and has a return type that is either 'void' or compatible with `FlowVar[T]`.

  ソース 編集