I noticed that I cant't use ref variable in thread even with orc (it said that 
with orc we have shared memory) For example
    
    
    var workers: array[10, Thread[void]]
    
    var name = "Hey"
    
    
    proc printer() {.gcsafe.} =
        echo name
    
    for i in 0..high(workers):
        createThread(workers[i], printer)
    
    joinThreads(workers)
    
    echo "Hello, World!"
    
    
    
    Run

Compile like this `nim c -r --threads:on --mm:orc main.nim`

And get such an error `Error: 'printer' is not GC-safe as it accesses 'name' 
which is a global using GC'ed memory`

I can fix it like this
    
    
    proc printer() =
        {.cast(gcsafe).}:
            withLock myLock:
                echo name
    
    
    Run

My question If I use `{.cast(gcsafe).}` and use lock around shared variable 
will I get safe code ? Or I still can mess reference counter of a shared 
variable?

And Is it valid way of accessing shared variables? For example if I want to use 
hash table as cache between threads 

Reply via email to