tsendtwice.nim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. discard """
  2. matrix: "--mm:refc"
  3. """
  4. # bug #4776
  5. import tables, algorithm
  6. type
  7. Base* = ref object of RootObj
  8. someSeq: seq[int]
  9. baseData: array[40000, byte]
  10. Derived* = ref object of Base
  11. data: array[40000, byte]
  12. type
  13. ThreadPool = ref object
  14. threads: seq[ptr Thread[ThreadArg]]
  15. channels: seq[ThreadArg]
  16. TableChannel = Channel[TableRef[string, Base]]
  17. ThreadArg = ptr TableChannel
  18. var globalTable {.threadvar.}: TableRef[string, Base]
  19. globalTable = newTable[string, Base]()
  20. let d = new(Derived)
  21. globalTable.add("ob", d)
  22. globalTable.add("ob2", d)
  23. globalTable.add("ob3", d)
  24. proc `<`(x, y: seq[int]): bool = x.len < y.len
  25. proc kvs(t: TableRef[string, Base]): seq[(string, seq[int])] =
  26. for k, v in t.pairs: result.add (k, v.someSeq)
  27. result.sort
  28. proc testThread(channel: ptr TableChannel) {.thread.} =
  29. globalTable = channel[].recv()
  30. var myObj: Base
  31. deepCopy(myObj, globalTable["ob"])
  32. myObj.someSeq = newSeq[int](100)
  33. let table = channel[].recv() # same table
  34. assert(table.contains("ob")) # fails!
  35. assert(table.contains("ob2")) # fails!
  36. assert(table.contains("ob3")) # fails!
  37. assert table.kvs == globalTable.kvs # Last to see above spot checks first
  38. var channel: TableChannel
  39. proc newThreadPool(threadCount: int) = #: ThreadPool =
  40. #new(result)
  41. #result.threads = newSeq[ptr Thread[ThreadArg]](threadCount)
  42. #var channel = cast[ptr TableChannel](allocShared0(sizeof(TableChannel)))
  43. channel.open()
  44. channel.send(globalTable)
  45. channel.send(globalTable)
  46. #createThread(threadPtr[], testThread, addr channel)
  47. testThread(addr channel)
  48. #result.threads[i] = threadPtr
  49. proc stop(p: ThreadPool) =
  50. for t in p.threads:
  51. joinThread(t[])
  52. dealloc(t)
  53. newThreadPool(1)#.stop()