tsendtwice.nim 1.6 KB

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