Thanks!  Here's what I did in shared-model.rkt:

(define *db* (box #f))
(define (get-in-memory-results)
  (let ([in-mem (unbox *db*)])
        (if in-mem
            (begin (displayln "database: cache hit")
                   in-mem)
            (begin (displayln "database: cache miss")
                   (set-box! *db* "initial data")
                   (unbox *db*)))))

(define (set-in-memory-results! s)
  (set-box! *db* s))

The only place in

  https://docs.racket-lang.org/reference/boxes.html

that mentions the word "atomic" is in box-cas!

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

How would I know box mutation is atomic?

Is structure mutation atomic?  After sending my previous message, I
remembered reading about structure mutation in How to Design Programs.
I looked it up again and had written this following code, which also
worked as I expected --- but I don't know if the mutation is atomic.

(define-struct database (data) #:mutable #:transparent)
(define *db* (make-database #f))

(define (get-in-memory-results)
  (let ([in-mem (database-data *db*)])
    (if (string? in-mem)
        (begin (displayln "database: cache hit")
               in-mem)
        (begin (displayln "database: cache miss")
               (set-database-data! *db* "fresh data")
               (database-data *db*)))))

(define (set-in-memory-results! s)
  (set-database-data! *db* s))

On Tuesday, August 13, 2019 9:16 PM, Jay McCarthy <jay.mccar...@gmail.com> 
wrote:

> I think it is pretty simple.
>
>     (define (make-periodically-updating-value compute1)
>       (define the-data (box (compute1)))
>       (define the-updater-t
>         (thread
>          (λ ()
>            (let loop ()
>              (set-box! the-data (compute1))
>              (sleep interval)
>              (loop)))))
>       (λ ()
>         (unbox the-data)))
>
>     (define get-the-data/cache
>       (make-periodically-updating-value get-the-data/for-realsies))
>
>
> Box mutation is atomic, so you don't need locks or anything. It would
> be more complicated if you want to not compute it initially.
>
> -------------------------------------------------------------------------------------------------------------------------------------
>
> Jay McCarthy
> Associate Professor @ CS @ UMass Lowell
> http://jeapostrophe.github.io
> Vincit qui se vincit.
>
> On Tue, Aug 13, 2019 at 7:53 PM Wayne Harris wharr...@protonmail.com wrote:
>
> > Is there an example somewhere showing how this could be done? My wish is to 
> > have one thing (a thread or something) periodically updating data (say 
> > every 30 minutes) and all servlets handling http connections reading. It is 
> > perfectly fine for me if while something writes the data, everything else 
> > is blocked waiting.
> > Having said that, I think this might be getting out of my league. (I'm 
> > reading about events in the hope I can find a way.) I have very basic 
> > understanding of Racket's primitives; never done anything involving threads 
> > or any kind of concurrency. If there's no trivial way to do that, I'll 
> > leave it for some other future project.
> > On Tuesday, August 13, 2019 5:07 PM, Jay McCarthy jay.mccar...@gmail.com 
> > wrote:
> >
> > > Hi Wayne,
> > > Your `in-memory-database` is a parameter. Parameters are
> > > thread-specific storage [1]. Every request in the web-server is
> > > handled by a different thread, so I think this will not work how you
> > > think it should.
> > > Jay
> > >
> > > 1.  
> > > https://docs.racket-lang.org/reference/parameters.html#(form._((lib._racket%2Fprivate%2Fmore-scheme..rkt)._parameterize))


-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/Cm8bvFYZUclwV5V1ClD-Sbs4XzxatJq43MwxAQpI2fB2qOfXLuTsedDO5ZiUlsSPFx8YskXTReXys6-BlA36DLg9Nm_FcNhIsm32cgCQwrI%3D%40protonmail.com.

Reply via email to