File: tsendtwice.nim

package info (click to toggle)
nim 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 462,356 kB
  • sloc: sh: 11,089; ansic: 4,699; makefile: 706; python: 309; sql: 297; asm: 141; xml: 13
file content (71 lines) | stat: -rw-r--r-- 1,702 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
discard """
  output: '''obj2 @[]
obj @[]
obj3 @[]
3
obj2 @[]
obj @[]
obj3 @[]'''
  cmd: "nim c -r --threads:on $file"
"""

# bug #4776

import tables

type
  Base* = ref object of RootObj
    someSeq: seq[int]
    baseData: array[400000, byte]
  Derived* = ref object of Base
    data: array[400000, byte]

type
  ThreadPool = ref object
    threads: seq[ptr Thread[ThreadArg]]
    channels: seq[ThreadArg]
  TableChannel = Channel[TableRef[string, Base]]
  ThreadArg = ptr TableChannel

var globalTable {.threadvar.}: TableRef[string, Base]
globalTable = newTable[string, Base]()
let d = new(Derived)
globalTable.add("obj", d)
globalTable.add("obj2", d)
globalTable.add("obj3", d)

proc testThread(channel: ptr TableChannel) {.thread.} =
  globalTable = channel[].recv()
  for k, v in pairs globaltable:
    echo k, " ", v.someSeq
  var myObj: Base
  deepCopy(myObj, globalTable["obj"])
  myObj.someSeq = newSeq[int](100)
  let table = channel[].recv() # same table
  echo table.len
  for k, v in mpairs table:
    echo k, " ", v.someSeq
  assert(table.contains("obj")) # fails!
  assert(table.contains("obj2")) # fails!
  assert(table.contains("obj3")) # fails!

var channel: TableChannel

proc newThreadPool(threadCount: int) = #: ThreadPool =
  #new(result)
  #result.threads = newSeq[ptr Thread[ThreadArg]](threadCount)
  #var channel = cast[ptr TableChannel](allocShared0(sizeof(TableChannel)))
  channel.open()
  channel.send(globalTable)
  channel.send(globalTable)
  #createThread(threadPtr[], testThread, addr channel)
  testThread(addr channel)
  #result.threads[i] = threadPtr

proc stop(p: ThreadPool) =
  for t in p.threads:
    joinThread(t[])
    dealloc(t)


newThreadPool(1)#.stop()