Source Edit

This module provides basic primitives for creating parallel programs. A Task should be only owned by a single Thread, it cannot be shared by threads.

Example:

  1. import std/tasks
  2. block:
  3. var num = 0
  4. proc hello(a: int) = inc num, a
  5. let b = toTask hello(13)
  6. b.invoke()
  7. assert num == 13
  8. # A task can be invoked multiple times
  9. b.invoke()
  10. assert num == 26
  11. block:
  12. type
  13. Runnable = ref object
  14. data: int
  15. var data: int
  16. proc hello(a: Runnable) {.nimcall.} =
  17. a.data += 2
  18. data = a.data
  19. when false:
  20. # the parameters of call must be isolated.
  21. let x = Runnable(data: 12)
  22. let b = toTask hello(x) # error ----> expression cannot be isolated: x
  23. b.invoke()
  24. let b = toTask(hello(Runnable(data: 12)))
  25. b.invoke()
  26. assert data == 14
  27. b.invoke()
  28. assert data == 16

Imports

macros, isolation, typetraits, effecttraits

Types

  1. Task = object

Task contains the callback and its arguments. Source Edit

Procs

  1. proc `=copy`(x: var Task; y: Task) {.error.}

Source Edit

  1. proc `=destroy`(t: var Task) {.inline, ...gcsafe, raises: [], tags: [RootEffect],
  2. forbids: [].}

Frees the resources allocated for a Task. Source Edit

  1. proc invoke(task: Task; res: pointer = nil) {.inline, ...gcsafe,
  2. raises: [Exception], tags: [RootEffect], forbids: [].}

Invokes the task. Source Edit

Macros

  1. macro toTask(e: typed{nkCall | nkInfix | nkPrefix | nkPostfix | nkCommand |
  2. nkCallStrLit}): Task

Converts the call and its arguments to Task.

Example:

  1. proc hello(a: int) = echo a
  2. let b = toTask hello(13)
  3. assert b is Task

Source Edit

Exports

unsafeIsolate, \=copy, \=sink, extract, \=destroy, Isolated, isolate