1
有沒有辦法中止從另一個線程的延續?在:如何中止從另一個線程的延續
#lang racket
(define thd
(thread (λ()
(call-with-continuation-prompt
(λ()
(sleep 10)
(printf "Don't print me.\n"))
(default-continuation-prompt-tag))
(printf "Do print me.\n"))))
(magically-abort-another-threads-continuation thd)
我希望不必使用線程控制。 作爲一個更具描述性的例子,說我想要做的實現只-而禮貌是這樣的:
#lang racket
(define i-am-polite #t)
(define-syntax-rule (do-only-while-polite body ...)
(call-with-continuation-prompt
(λ()
(define thd (current-thread))
(define politeness-checker-thd
(thread (λ()
(let loop()
(cond
[(not i-am-polite)
(magically-abort-another-threads-continuation thd)]
[else (sleep 0.1)
(loop)])))))
body ...
(thread-kill politeness-checker-thd))
(default-continuation-prompt-tag)))
(thread (λ()
(do-only-while-polite
(printf "Hello.\n")
(sleep 1)
(printf "How are you doing?")
(sleep 1)
(printf "It's nice to meet you."))
(printf "Bye.")))
(sleep 1)
(set! i-am-polite #f)
我給出的例子很粗糙。我所希望的是一種從另一個動態地控制線程流的方法,而不必產生額外的線程並使用kill-thread等。但也許使用線程是方式。 – vagn
線程可以通過通道相互通信,因此可以通過其他方式間接管理控制流。 – dyoo
我期望['break-thread'](http://docs.racket-lang.org/reference/threads.html#%28def._%28%28quote._~23~25kernel%29._break-線程%29%29)是一個更好的匹配,但是是的,一些適當的溝通比這更好,而且有些嘗試使用延續。 –