tsendtwice.nim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. discard """
  2. output: '''obj2 nil
  3. obj nil
  4. obj3 nil
  5. 3
  6. obj2 nil
  7. obj nil
  8. obj3 nil'''
  9. cmd: "nim c -r --threads:on $file"
  10. """
  11. # bug #4776
  12. import tables
  13. type
  14. Base* = ref object of RootObj
  15. someSeq: seq[int]
  16. baseData: array[400000, byte]
  17. Derived* = ref object of Base
  18. data: array[400000, byte]
  19. type
  20. ThreadPool = ref object
  21. threads: seq[ptr Thread[ThreadArg]]
  22. channels: seq[ThreadArg]
  23. TableChannel = Channel[TableRef[string, Base]]
  24. ThreadArg = ptr TableChannel
  25. var globalTable {.threadvar.}: TableRef[string, Base]
  26. globalTable = newTable[string, Base]()
  27. let d = new(Derived)
  28. globalTable.add("obj", d)
  29. globalTable.add("obj2", d)
  30. globalTable.add("obj3", d)
  31. proc testThread(channel: ptr TableChannel) {.thread.} =
  32. globalTable = channel[].recv()
  33. for k, v in pairs globaltable:
  34. echo k, " ", v.someSeq
  35. var myObj: Base
  36. deepCopy(myObj, globalTable["obj"])
  37. myObj.someSeq = newSeq[int](100)
  38. let table = channel[].recv() # same table
  39. echo table.len
  40. for k, v in mpairs table:
  41. echo k, " ", v.someSeq
  42. assert(table.contains("obj")) # fails!
  43. assert(table.contains("obj2")) # fails!
  44. assert(table.contains("obj3")) # fails!
  45. var channel: TableChannel
  46. proc newThreadPool(threadCount: int) = #: ThreadPool =
  47. #new(result)
  48. #result.threads = newSeq[ptr Thread[ThreadArg]](threadCount)
  49. #var channel = cast[ptr TableChannel](allocShared0(sizeof(TableChannel)))
  50. channel.open()
  51. channel.send(globalTable)
  52. channel.send(globalTable)
  53. #createThread(threadPtr[], testThread, addr channel)
  54. testThread(addr channel)
  55. #result.threads[i] = threadPtr
  56. proc stop(p: ThreadPool) =
  57. for t in p.threads:
  58. joinThread(t[])
  59. dealloc(t)
  60. newThreadPool(1)#.stop()