I want to move custom object to a thread (without channels by hand) I wrote 
this code
    
    
    import locks
    type
        MyString = object
            val: string
    
    var L: Lock
    
    proc `=destroy`(myStr: var MyString) =
        echo "Destroy '", myStr, "' ", getThreadId()
        `=destroy`(myStr.val)
    
    
    
    var myThread: Thread[void]
    var data {.guard: L.}: seq[MyString]
    
    
    initLock(L)
    
    
    proc printer() =
        {.cast(gcsafe).}:
            withLock L:
                var theStr = data.pop()
                echo "Printer: '", theStr.val, "' ", getThreadId()
    
    
    echo "Main start: ", getThreadId()
    
    var myStr = MyString(val: "Hello, World!")
    withLock L:
        data.add(myStr)
    
    createThread(myThread, printer)
    joinThread(myThread)
    
    deinitLock(L)
    echo "Main finish: ", getThreadId()
    
    
    
    Run

And the output is
    
    
    Main start: 462466
    Destroy '(val: "")' 462476
    Printer: 'Hello, World!' 462476
    Destroy '(val: "Hello, World!")' 462476
    Main finish: 462466
    Destroy '(val: "Hello, World!")' 462466
    
    
    Run

I have double destroy here. Why doesn't myStr moved (sinked) to seq ?

Or should I after adding to seq call wasMoved?

And what that empty `Destroy '(val: "")' 462476` in thread?

Maybe you can show correct way to move value to thread? 

Reply via email to