Hi Craig Allen
Would something like the following work?
Jos
 
(define (make-sema n)
 (define-syntax-rule (with-counter-sema expr ...)
  (begin
   (semaphore-wait counter-sema)
   (define result (begin expr ...))
   (semaphore-post counter-sema)
   result))
 (define counter-sema (make-semaphore 1))
 (define counter n)
 (define sema (make-semaphore n))
 (define (wait) (with-counter-sema (semaphore-wait sema) (set! n (sub1 n))))
 (define (post) (with-counter-sema (semaphore-post sema) (set! n (add1 n))))
 (define (count) (with-counter-sema n))
 (values wait post count))
 
(define-values (wait post count) (make-sema 3))
(count)
(wait)
(count)
(post)
(count)


  _____  

From: racket-users@googlegroups.com [mailto:racket-users@googlegroups.com] On 
Behalf Of George Neuner
Sent: 04 September 2018 17:47
To: Craig Allen
Cc: racket users
Subject: Re: [racket-users] Semaphore-count



On 9/4/2018 8:31 AM, Craig Allen wrote:


I saw that function, but was scared off by its documentation:

Like semaphore-wait, but semaphore-try-wait? never blocks execution. If sema's 
internal counter is zero, semaphore-try-wait? returns
#f immediately without decrementing the counter. If sema's counter is positive, 
it is decremented and #t is returned.


I'm not sure why I'd want it to decrement the count, I really just want to see 
if my critical section is running. I will try it
though, thanks.




'wait' takes the semaphore when it is available.  'try-wait' takes the 
semaphore if possible, or fails immediately if it can't.  

AFAIK there is no way to simply 'check' if the semaphore is available.  In a 
multithread environment, such checking doesn't do much
good anyway ... a positive counter value doesn't mean you actually can take a 
semaphore - some other thread(s) may beat you to it.

The canon [language independent] methods of "optimistic" locking are either to 
spin until 'try' returns true, or to combine one or a
few initial probes using 'try' with a fall back to a normal wait if the probes 
fail.

George


-- 
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
<mailto:racket-users+unsubscr...@googlegroups.com> 
racket-users+unsubscr...@googlegroups.com.
For more options, visit  <https://groups.google.com/d/optout> 
https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to