tthreadanalysis.nim 1004 B

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