Did you look at using a semaphore? That's what I would try, but I also didn't know about box-cas!

Thanks,

Dave


On 09/01/2016 10:49 AM, Erich Rast wrote:
Wow, I didn't know about box-cas! That seems to be a good solution in
this case. Thanks!

-- Erich

On Thu, 1 Sep 2016 09:37:06 -0300
Gustavo Massaccesi <gust...@oma.org.ar> wrote:

What about box-cas! ?
https://docs.racket-lang.org/reference/boxes.html#%28def._%28%28quote._~23~25kernel%29._box-cas%21%29%29

Something like:
;---

#lang racket
(define handle-counter (box 1))
(define (new-handle)
   (define old (unbox handle-counter))
   (define new (add1 old))
   (unless (box-cas! handle-counter old new)
     (new-handle))
   old)    ; or new ?
;---

Gustavo



On Thu, Sep 1, 2016 at 6:44 AM, Erich Rast <er...@snafu.de> 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.

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