Best way to say 'block until true'?

29 views
Skip to first unread message

David Storrs

unread,
Mar 19, 2021, 11:59:44 AM3/19/21
to Racket Users
Suppose I have a function that tests for some condition, e.g.

(define current-user (make-parameter #f))
(define (current-user-set?) (not (false? (current-user)))

What is the best way to say "wait until 'current-user-set?' returns true"?  I've been through the Events chapter in the Reference and nothing seems like a great fit.  I could do polling via sleep or alarm-evt but that seems inefficient.  Is there a better way?

Jay McCarthy

unread,
Mar 19, 2021, 12:02:20 PM3/19/21
to David Storrs, Racket Users
The best thing is to use a semaphore instead of a mutable reference.
If you can't do that, then I think that you should combine the mutable
reference with a signaling semaphore. If you can't do that, then I
can't think of anything but a poll.

--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit.
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKocbPgjcFAF_o2g6mhZBEH8PpeGyJ4CwznKc3DZkMjY%3DGw%40mail.gmail.com.

Sam Tobin-Hochstadt

unread,
Mar 19, 2021, 12:15:11 PM3/19/21
to Jay McCarthy, David Storrs, Racket Users
Another possibility is to send a message on a channel when the user is
set, and then just wait with `sync` for a message to appear on the
channel.

Sam
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAJYbDanE4zqgFRAFSYs4kdLzjKf9xg3xi0JMNU7VmFREstNBgQ%40mail.gmail.com.

David Storrs

unread,
Mar 19, 2021, 12:21:19 PM3/19/21
to Sam Tobin-Hochstadt, Jay McCarthy, Racket Users
Cool.  Thank you both.

David Storrs

unread,
Mar 19, 2021, 1:02:53 PM3/19/21
to Jay McCarthy, Racket Users
On Fri, Mar 19, 2021 at 12:02 PM Jay McCarthy <jay.mc...@gmail.com> wrote:
The best thing is to use a semaphore instead of a mutable reference.
If you can't do that, then I think that you should combine the mutable
reference with a signaling semaphore. If you can't do that, then I
can't think of anything but a poll.

Is this the kind of thing you meant by 'signalling semaphore'?

#lang racket

(define x #f)
(define sema (make-semaphore 0))

(define (wait-on-x) (sync (semaphore-peek-evt sema)) always-evt)
(define (set-x! val)
  (void (thread ; don't print the thread object when running in the repl                      
         (thunk
          (sleep 3)
          (set! x val)
          (semaphore-post sema)))))

(define (check-x)
  (match (sync/timeout 10 (wait-on-x))
    [#f (displayln "timeout")]
    [_ (displayln "success")]))

(set-x! 7)
(check-x)  ; pauses for 3 seconds, then outputs "success"
(check-x)  ; outputs "success" immediately
(check-x)  ; ibid

Jay McCarthy

unread,
Mar 19, 2021, 1:33:02 PM3/19/21
to David Storrs, Racket Users
It is not a built-in thing. I am talking about the use-pattern of a
condition variable:
https://en.wikipedia.org/wiki/Monitor_(synchronization)#Condition_variables

--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit.

David Storrs

unread,
Mar 19, 2021, 1:51:55 PM3/19/21
to Jay McCarthy, Racket Users
Ah. Thanks.
Reply all
Reply to author
Forward
0 new messages