Skip to content

Instantly share code, notes, and snippets.

@treeform
Created January 13, 2020 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treeform/b8abfeae9038ba64af35724e7b1a0386 to your computer and use it in GitHub Desktop.
Save treeform/b8abfeae9038ba64af35724e7b1a0386 to your computer and use it in GitHub Desktop.
# createThread then .add -> crash
# .add then createThread -> some threads dont run
# pre .add all threads -> works 100%
import locks
var
thr: seq[Thread[void]]
L: Lock
proc threadFunc() {.thread.} =
acquire(L)
echo "exit"
release(L)
initLock(L)
# # crashes:
# for i in 0 ..< 5:
# var t: Thread[void]
# createThread(t, threadFunc)
# thr.add t
# # strange behavior, not all threads run:
# for i in 0 ..< 5:
# var t: Thread[void]
# thr.add t
# createThread(t, threadFunc)
for i in 0 ..< 5:
var t: Thread[void]
thr.add t
for i in 0 ..< 5:
createThread(thr[i], threadFunc)
joinThreads(thr)
echo "exit final"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment