[racket] Different mutable vectors for each thread
I think thread-cells are enough, you just need to do the allocation yourself.
#lang racket
(define get-thread-local-vector
(let ()
(define vector-cell (make-thread-cell #f))
(lambda ()
(or (thread-cell-ref vector-cell)
(let ((vec (vector 0 1)))
(thread-cell-set! vector-cell vec)
vec)))))
(vector-set! (get-thread-local-vector) 0 'new-value)
(get-thread-local-vector)
(void (sync (thread (lambda () (print (get-thread-local-vector)) (newline)))))
=>
'#(new-value 1)
'#(0 1)
On Sat, Jul 14, 2012 at 10:50 PM, Neil Toronto <neil.toronto at gmail.com> wrote:
> To head off wrong answers: It's not thread cells. By themselves, they're not
> enough. Making them "preserved" also isn't the answer.
>
> I'd like a thread-cell-like thing that gives each thread its own mutable
> vector. Here's why thread cells don't work:
>
> #lang racket
>
> (define v (make-thread-cell (vector 0 1)))
> (sync (thread (λ () (vector-set! (thread-cell-ref v) 0 6))))
> (thread-cell-ref v)
>
>
> Output:
>
> #<thread>
> '#(6 1)
>
>
> Clearly, both the created thread and the runtime thread access the same
> vector.
>
> Not only do I need this, but I need repeated access to the per-thread
> vectors to be faster than allocating a new one. Ideally, after the first
> access, they'd allocate nothing.
>
> Any ideas?
>
> Neil ⊥
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users