chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Thread safe hash tables?


From: Kon Lovett
Subject: Re: [Chicken-users] Thread safe hash tables?
Date: Sat, 4 Mar 2006 23:37:44 -0800

On Mar 4, 2006, at 1:05 PM, Thomas Chust wrote:

On Sat, 4 Mar 2006, KonLovett wrote:

On Mar 4, 2006, at 12:07 PM, Thomas Chust wrote:

On Sat, 4 Mar 2006, KonLovett wrote:
> On Mar 2, 2006, at 4:37 AM, Thomas Chust wrote:
> [...]
> FWIW my reading of the source is 1) No & 2) Yes. You could surround > access w/ disable/enable interrupts, since that would inhibit a context > switch by the schedular during the access. But I think creating a > "thread-safe" wrapper around your shared data-structure, as you suggest, > is the best.
> [...]
[...]
so you think I can mess up the internal data structure of a hash table if I access it concurrently from two threads? [...] But thank you for telling me, because after some tests I thought the hash tables *were* thread safe.

Well, testing tells. I just don't see, after cursory inspection (the FWIW above), anything to stop a context switch during processing of a hash-table mutator (see 'extras.scm'). But, I would ask Felix for an authoritative answer.

Hello,

testing multithreaded stuff has so many quirks that I'll rather play it safe. And you are right that the source in extras.scm doesn't look like it had any special precautions installed. I've added the locks to my code now, because I found out that they would also prevent another exotic problem scenario in a multithreaded environment.

In any case, macros can do wonders for those bazillion nasty code sequences ;)

Maybe a macro like this one should be added to CHICKEN:

    (define-macro (with-locked-mutex-specific mtx proc)
      (let ((mtx-var (gensym 'mtx)))
        `(let ((,mtx-var mtx))
         (dynamic-wind
             (lambda () (mutex-lock! ,mtx-var))
             (lambda () (,proc (mutex-specific ,mtx-var)))
             (lambda () (mutex-unlock! ,mtx-var))))))

I've used something like that every second time I wrote a multithreaded program in CHICKEN so I think it could be useful as a standard extension ;)

Yes. Maybe:

(define-macro (call/synch MTX PROC)
        (let ((MTX-VAR (gensym 'MTX)))
                `(let ((,MTX-VAR ,MTX))
                        (dynamic-wind
                                (lambda () (mutex-lock! ,MTX-VAR))
                                (lambda () (,PROC (mutex-specific ,MTX-VAR)))
                                (lambda () (mutex-unlock! ,MTX-VAR))))))

(define-macro (apply/synch MTX PROC . REST)
        (let ((MTX-VAR (gensym 'MTX)))
                `(let ((,MTX-VAR ,MTX))
                        (dynamic-wind
                                (lambda () (mutex-lock! ,MTX-VAR))
                                (lambda () (apply ,PROC ,@REST))
                                (lambda () (mutex-unlock! ,MTX-VAR))))))

(define-macro (synch MTX . BODY)
        (let ((MTX-VAR (gensym 'MTX)))
                `(let ([,MTX-VAR ,MTX])
                        (dynamic-wind
                                (lambda () (mutex-lock! ,MTX-VAR))
                                (lambda () ,@BODY)
                                (lambda () (mutex-unlock! ,MTX-VAR))))))


[...]

cu,
Thomas





reply via email to

[Prev in Thread] Current Thread [Next in Thread]