tthreadanalysis2.nim 1011 B

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