[concurrency-interest] Concurrent Bidirectional Map

Norman Elton normelton at gmail.com
Sun Nov 22 20:19:18 EST 2009


> Yea, the update to both maps is not atomic because reader threads can
> interleave between writes to either map. Technically, you won't corrupt the
> map (unlike in the case of a regular HashMap), but that depends on what your
> definition of corruption is (as David says). A read from the value->key map
> can be inconsistent with a put to the key->value map.

Thanks David & Dhanji for your thinking on this.

I realize that if two threads hit the map at the same time, one doing
a look-by-key and another doing a lookup-by-value, while a third
thread is doing an update, it is possible the two readers will see
different pairings. But, is this a problem? They are two separate
threads. How should they expect to be simultaneously seeing the same
"state" of the map?

Likewise, a single thread that does a lookup-by-key, followed by a
lookup-by-value, should expect to get two different results, since
it's entirely feasible that another thread updates the map between the
two reads.

My implementation of the BiMap doesn't have any unsynchronized methods
(I think!) that refer to both maps. All of the reader methods use a
single of the two CHMs, which represent a valid "snapshot" of the map
when referenced.

Thanks again, all, for your help on this! Let me know if my reasoning
is flawed, I've enjoyed thinking though this :-).

Norman


More information about the Concurrency-interest mailing list