Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-methods don't work when passing ref to a different thread #4689

Closed
endragor opened this issue Aug 31, 2016 · 0 comments
Closed

Multi-methods don't work when passing ref to a different thread #4689

endragor opened this issue Aug 31, 2016 · 0 comments

Comments

@endragor
Copy link
Contributor

endragor commented Aug 31, 2016

Sample:

import tables

type
  Base* = ref object of RootObj
  Derived* = ref object of Base

method someMethod(self: Base) =
  echo "Base"
method someMethod(self: Derived) =
  echo "Derived"

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

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

proc testThread(channel: ptr TableChannel) {.thread.} =
  globalTable = channel[].recv()
  globalTable["obj"].someMethod()

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

discard newThreadPool(8)

This code should print Derived 8 times, but nothing gets printed, even though someMethod invocation is successful (no crash or anything). It looks as if nothing at all gets invoked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant