Events: Difference between revisions

(Add Rust implementation)
Line 697:
echo "received signal from pipe"</lang>
 
===Stdlib Semaphore===
This version using locks module for signaling the condition.
 
<lang nim>import locks
from os import sleep
from times import cpuTime
from strformat import fmt
 
var
# condition variable which shared across threads
cond: Cond
lock: Lock
threadproc: Thread[void]
 
proc waiting {.thread.} =
echo "spawned waiting proc"
let start = cpuTime()
cond.wait lock
echo fmt"thread ended after waiting: {cpuTime() - start} seconds."
 
proc main =
initCond cond
initLock lock
threadproc.createThread waiting
echo "in main proc"
os.sleep 1000
echo "send signal/event notification"
signal cond
joinThread threadproc
deinitCond cond
deinitLock lock
 
main()</lang>
 
Compile and run: <pre>nim c -r --threads:on events_cond.nim</pre>
{{out}}
<pre>in main proc
spawned waiting proc
send signal/event notification
thread ended after waiting: 1.001 seconds.</pre>
 
=={{header|Oforth}}==
Anonymous user