tgc_unsafe2.nim 947 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. discard """
  2. line: 28
  3. nimout: '''tgc_unsafe2.nim(22, 6) Warning: 'trick' is not GC-safe as it accesses 'global' which is a global using GC'ed memory [GcUnsafe2]
  4. tgc_unsafe2.nim(26, 6) Warning: 'track' is not GC-safe as it calls 'trick' [GcUnsafe2]
  5. tgc_unsafe2.nim(28, 6) Error: 'consumer' is not GC-safe as it calls 'track'
  6. '''
  7. errormsg: "'consumer' is not GC-safe as it calls 'track'"
  8. """
  9. import threadpool
  10. type StringChannel = Channel[string]
  11. var channels: array[1..3, StringChannel]
  12. type
  13. MyObject[T] = object
  14. x: T
  15. var global: MyObject[string]
  16. var globalB: MyObject[float]
  17. proc trick(ix: int) =
  18. echo global.x
  19. echo channels[ix].recv()
  20. proc track(ix: int) = trick(ix)
  21. proc consumer(ix: int) {.thread.} =
  22. track(ix)
  23. proc main =
  24. for ix in 1..3: channels[ix].open()
  25. for ix in 1..3: spawn consumer(ix)
  26. for ix in 1..3: channels[ix].send("test")
  27. sync()
  28. for ix in 1..3: channels[ix].close()
  29. when isMainModule:
  30. main()