@Mikra, Depending on the compiler, _unspecified_ usually means asking for 
trouble and lots of sleepless nights!  lol

With the above code, I lose both signals, and always get a deadlock, even with 
a single wait.

I believe this is because the signals happen 5000ms before the main thread 
waits on the condition, and the signals are not saved.

If I reverse the roles of the threads, like the code below, everything works, 
no deadlocks.

But, if I un-comment the second wait, I deadlock, the second signal is lost. 
(Again, I expected this to happen.)

If I then place a sleep between the two signals in the following code, no 
deadlock, no lost signals.

All of these results enforce my understanding that signals are never saved.

I don't have a windows box atm, but I will in a few days, and I will test it 
there. Maybe Microsoft did weird stuff with their implementation of condition 
variables?
    
    
    import locks,os
    
    # globals
    var
      thr: Thread[int] # donĀ“t know if there is a untyped thread possible
      cLock: Lock
      lockCond: Cond
    
    proc threadFunc (param : int) {.thread.} =
      echo "start waiting"
      withLock(cLock):
        wait(lockCond,cLock)
        #wait(lockCond,cLock)
    # uncomment to check if there is a queue behind; on windows the (expected)
    # behaviour is a deadlock for the second wait (second signal call is lost)
      echo "end waiting"
    
    initLock(cLock)
    initCond(lockCond)
    
    createThread(thr, threadFunc,0)
    
    echo "mainthread:begin"
    sleep(5000) # ensure that the signal of the childthread is called first
    
    signal(lockCond) # first signal call
    signal(lockCond) # if you signal twice the second signal is lost (no queue)
    echo "mainthread: signal executed"
    
    joinThreads(thr) # should not block if the childthread is already finished
    deinitLock(cLock)
    deinitCond(lockCond)
    echo "end"
    

Reply via email to