How about using channels:

#lang racket
(define handle-channel (make-channel))

(define (handle-producer ch)
  (let loop ((counter 1))
    (channel-put ch counter)
    (loop (add1 counter))))

(thread (lambda () (handle-producer handle-channel)))

(define (new-handle)
  (channel-get handle-channel))

Alex.

On Thursday, September 1, 2016 at 5:44:30 PM UTC+8, erich wrote:
> This question is also about style. Take an admittedly ugly
> counter like this
> 
> ;; increasing counter for handles
> (define handle-counter 1)
> 
> (define (new-handle)
>   (begin0
>     handle-counter
>     (set! handle-counter (add1 handle-counter))))
> 
> Various threads may call (new-handle) concurrently, so I need to
> ensure that every thread gets a unique handle. However, the handles need
> not reflect the temporal order of the calling sequence in any way. It's
> hard to debug but I believe I've seen a case in which the above code
> has assigned the same handle to concurrent threads, as one would expect.
> 
> So basically new-handle needs to be in a critical section, right? I feel
> stupid that I don't know the answer, but how do I do this?
> 
> In the docs I've found ffi/unsafe/atomic, but this seems like overkill
> and is unsafe and only intended for the FFI.
> 
> Second question: Should I use a parameter or something else for this
> construct, and if so, what are the benefits of doing so? What would be
> the 'right' way of providing unique numbers in a synchronized way?
> 
> Best,
> 
> Erich

-- 
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