I'm trying to use locks and condition variable from the locks module, the 
documentation I found [here](https://nim-lang.org/docs/locks.html) seems to be 
quite poor, then I assumed that condition variables works like 
std::condition_variable in C++ that I've already used in the past. In 
particular I assumed that the wait function needs to be called on a Cond while 
holding a lock and this causes the thread to be stopped and the locks released 
until another thread calls signal on the Cond. This seems not to be the case 
since this very basic example results in a deadlock.

What's wrong with it and how does Cond works?
    
    
    import locks
    import deques
    import cpuinfo
    
    
    var
      thr = newSeq[Thread[int]](countProcessors())
      lock: Lock
      cond : Cond
    
    proc threadFunc(i: int) {.thread.} =
        withLock(lock):
            cond.wait(lock)
            echo i
    
    initLock(lock)
    initCond(cond)
    
    withLock(lock):
        for i in 0..<thr.len:
            createThread(thr[i], threadFunc, i)
    
    for i in 0..<thr.len:
        cond.signal()
    
    for i in 0..<thr.len:
        joinThreads(thr)
    

Reply via email to