CreateWaitableTimer. Then found out both needs some special treatment. For example, POSIX
timer_createrequires signal handling which is lacking on Sagittarius. (Honestly, I've never properly understood how signal masking works...)
So I've wrote sort of mimic code with thread.
(import (rnrs) (srfi :18)) ;; simple timer (define-record-type (Above prints
make-timer timer?) (fields (immutable thread timer-thread)) (protocol (lambda (p) (lambda (interval thunk) (p (make-thread (lambda () (let loop () (thread-sleep! interval) (thunk) (loop))))))))) (define (timer-start! timer) (thread-start! (timer-thread timer)) timer) (define (timer-cancel! timer) (thread-terminate! (timer-thread timer))) ;; use it (define t (timer-start! (make-timer 2 (lambda () (print "It's time!!"))))) (define (heavy-to-do) (thread-sleep! 5) (print "It was heavy!")) (heavy-to-do)
It's time!!twice then finish
heavy-to-do. Now I'm wondering if this is enough or not. Without deep consideration, I've got couple of pros and cons with this implementation.
- Easy to implement and could be portable.
- Could be expensive. (Making thread is not cheap on Sagittarius)
- Timer can't change parameters which is thread local.
SetWaitableTimercan also take a callback function. And according to MSDN, the callback function will be executed on the same thread.
The completion routine will be executed by the same thread that called SetWaitableTimer. This thread must be in an alertable state to execute the completion routine. It accomplishes this by calling the SleepEx function, which is an alertable function.Now, I'm not sure what's alertable state exactly means. Seems the target thread should be sleeping and if so, sucks...
Using Waitable Timers with an Asynchronous Procedure Call
Hmmmm, it may not an easy thing to do.