Threads

The --threads:on command-line switch is enabled by default. The typedthreads module module then contains several threading primitives. See spawn for further details.

The only ways to create a thread is via spawn or createThread.

Thread pragma

A proc that is executed as a new thread of execution should be marked by the thread pragma for reasons of readability. The compiler checks for violations of the no heap sharing restriction: This restriction implies that it is invalid to construct a data structure that consists of memory allocated from different (thread-local) heaps.

A thread proc can be passed to createThread or spawn.

Threadvar pragma

A variable can be marked with the threadvar pragma, which makes it a thread-local variable; Additionally, this implies all the effects of the global pragma.

  1. var checkpoints* {.threadvar.}: seq[string]

Due to implementation restrictions, thread-local variables cannot be initialized within the var section. (Every thread-local variable needs to be replicated at thread creation.)

Threads and exceptions

The interaction between threads and exceptions is simple: A handled exception in one thread cannot affect any other thread. However, an unhandled exception in one thread terminates the whole process.