tgc_unsafe2.nim 928 B

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