[plt-scheme] concurrent modification of hashtables

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Jun 3 16:13:02 EDT 2003

At Fri, 30 May 2003 16:55:02 -0400 (EDT), "David A. Herman" wrote:
> Helpdesk mentions concurrent modification of hash-tables in the presence
> of multiple threads, but it doesn't talk about patterns such as:
> 
> (define (hash-table-filter! t p)
>   (hash-table-for-each t
>     (lambda (key value)
>       (unless (p key value)
>         (hash-table-remove! t key)))))
> 
> In other words, can a function currently iterating across a hashtable
> safely modify the current entry it's observing? (A Java analog would be
> java.util.Iterator.remove().)

Though I hadn't considered it before, it turns out that removing is
always ok. If you've already seen the key, it's ok, and if you haven't
seen it yet, you won't. Perhaps this implementation artifact should be
turned into a guarantee.

Meanwhile, adding values in the `hash-table-for-each' procedure is as
bad as a concurrent modification. I'll fix the docs.

Matthew



Posted on the users mailing list.