Source Edit

Thread support for Nim.

Examples

  1. import std/locks
  2. var
  3. thr: array[0..4, Thread[tuple[a,b: int]]]
  4. L: Lock
  5. proc threadFunc(interval: tuple[a,b: int]) {.thread.} =
  6. for i in interval.a..interval.b:
  7. acquire(L) # lock stdout
  8. echo i
  9. release(L)
  10. initLock(L)
  11. for i in 0..high(thr):
  12. createThread(thr[i], threadFunc, (i*10, i*10+5))
  13. joinThreads(thr)
  14. deinitLock(L)

Imports

threadtypes, ansi_c

Procs

  1. proc createThread(t: var Thread[void]; tp: proc () {.thread, nimcall.}) {.
  2. ...raises: [ResourceExhaustedError], tags: [], forbids: [].}

Source Edit

  1. proc createThread[TArg](t: var Thread[TArg];
  2. tp: proc (arg: TArg) {.thread, nimcall.}; param: TArg)

Creates a new thread t and starts its execution.

Entry point is the proc tp. param is passed to tp. TArg can be void if you don’t need to pass any data to the thread.

Source Edit

  1. proc getThreadId(): int {....raises: [], tags: [], forbids: [].}

Gets the ID of the currently running thread. Source Edit

  1. proc handle[TArg](t: Thread[TArg]): SysThread {.inline.}

Returns the thread handle of t. Source Edit

  1. proc joinThread[TArg](t: Thread[TArg]) {.inline.}

Waits for the thread t to finish. Source Edit

  1. proc joinThreads[TArg](t: varargs[Thread[TArg]])

Waits for every thread in t to finish. Source Edit

  1. proc pinToCpu[Arg](t: var Thread[Arg]; cpu: Natural)

Pins a thread to a CPU.

In other words sets a thread’s affinity. If you don’t know what this means, you shouldn’t use this proc.

Source Edit

  1. proc running[TArg](t: Thread[TArg]): bool {.inline.}

Returns true if t is running. Source Edit

Exports

Thread