[ create a new paste ] login | about

Link: http://codepad.org/vpN39iyg    [ raw code | fork ]

Scheme, pasted on Jul 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
;; Returns the value of (thunk), timing out
;; after 'seconds' seconds and returning the given default
;; if the thunk takes too long to run.
(define (timeout-exec seconds thunk default)
  (call-in-nested-thread (lambda ()
			   (define mainthread (current-thread))
			   (define execthread
			     (thread (lambda ()
				       (thread-send mainthread (thunk)))))
			   (define watchdog
			     (thread (lambda ()
				       (sleep seconds)
				       (and (thread-running? execthread)
					    (begin (kill-thread execthread)
						   (thread-send mainthread default))))))
			   (thread-receive))))


Create a new paste based on this one


Comments: