There are two kinds of time-out events. The timeOutEvt function returns an event that will delay each time that it is synchronised on. The atTimeEvt function returns an event that becomes enabled at some time in the future and then is always enabled thereafter.
These differences are illustrated by the timeouts program. Depending on the command line argument it will exercise either of the abs or delta functions.
Here is the delta function.
fun delta() = let val tmevt = CML.timeOutEvt (Time.fromSeconds 5) in print "Waiting for the timeout\n"; CML.sync tmevt; print "Delay\n"; CML.sync (CML.timeOutEvt (Time.fromSeconds 1)); print "Waiting again for the timeout\n"; CML.sync tmevt; print "Done\n" end |
When you run the program with the delta command-line argument you will see that the "Waiting for the timeout" message is followed by a 5 second delay each time.
Here is the abs function.
fun abs() = let val when = Time.+(Time.now(), Time.fromSeconds 5) val tmevt = CML.atTimeEvt when in print "Waiting for the timeout\n"; CML.sync tmevt; print "Delay\n"; CML.sync (CML.timeOutEvt (Time.fromSeconds 2)); print "Waiting again for the timeout\n"; CML.sync tmevt; print "Delay again\n"; CML.sync (CML.timeOutEvt (Time.fromSeconds 2)); print "Waiting again for the timeout\n"; CML.sync tmevt; print "Done\n" end |
When you run the program with the abs command-line argument you will see that there is a 5 second delay the first time but then each subsequent wait takes no time. The timeout event is always enabled once the target time has been reached. This will be useful in programs to produce an abort indication after a time-out.