Thanks, I saw your examples in previous posts with similar theme (I've post
link before). Well, I did "simple" application, who demonstrate exchanging
process of object between two threads. This model looks it is logical, but code
looks really mad and inflexible (for 21th), sorry:
import os, threadpool
type
SharedChannel[T] = ptr Channel[T]
proc newSharedChannel[T](): SharedChannel[T] =
result = cast[SharedChannel[T]](allocShared0(sizeof(Channel[T])))
open(result[])
proc close[T](ch: var SharedChannel[T]) =
close(ch[])
deallocShared(ch)
ch = nil
proc send[T](ch: SharedChannel[T], content: T) =
ch[].send(content)
proc recv[T](ch: SharedChannel[T]): T =
result = ch[].recv
type
MyObj = ref object
flag: bool
proc someThread(ch: SharedChannel[MyObj]) {.thread.} =
var obj = ch.recv
if obj != nil:
echo obj.flag
obj.flag = false
ch.send(obj)
var
ch = newSharedChannel[MyObj]()
spawn someThread(ch)
var obj: MyObj
obj.new()
obj.flag = true
ch.send(obj)
sleep 500
obj = ch.recv
echo obj.flag
close(ch)
Now I'll figure out how best to integrate this model into the application.
Thanks for all for your help and discussion!