tthreadanalysis.nim 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. discard """
  2. errormsg: "'threadFunc' is not GC-safe"
  3. line: 38
  4. cmd: "nim $target --hints:on --threads:on $options $file"
  5. """
  6. import os
  7. var
  8. thr: array[0..5, Thread[tuple[a, b: int]]]
  9. proc doNothing() = discard
  10. type
  11. PNode = ref TNode
  12. TNode {.pure.} = object
  13. le, ri: PNode
  14. data: string
  15. var
  16. root: PNode
  17. proc buildTree(depth: int): PNode =
  18. if depth == 3: return nil
  19. new(result)
  20. result.le = buildTree(depth-1)
  21. result.ri = buildTree(depth-1)
  22. result.data = $depth
  23. proc echoLeTree(n: PNode) =
  24. var it: PNode
  25. it = nil
  26. it = n
  27. while it != nil:
  28. echo it.data
  29. it = it.le
  30. proc threadFunc(interval: tuple[a, b: int]) {.thread.} =
  31. doNothing()
  32. for i in interval.a..interval.b:
  33. var r = buildTree(i)
  34. echoLeTree(r) # for local data
  35. echoLeTree(root) # and the same for foreign data :-)
  36. proc main =
  37. root = buildTree(5)
  38. for i in 0..high(thr):
  39. createThread(thr[i], threadFunc, (i*3, i*3+2))
  40. joinThreads(thr)
  41. main()