Michael van der Gulik wrote:
> A thread-safe collection package could have non-blocking implementations 
> of most methods. Writing an object reference into an array should be an 
> atomic operation and I assume most collections use Arrays for their 
> implementation.

Write to an array is atomic, but the sequence of

   1. Look in array to see if entry is used, and if unused

   2. Store into array

is not atomic, and hashed collections like Set, Dictionary, etc. require 
this sequence, usually with additional non-atomic operations on write, 
and sometimes on read.

It's difficult to make a collection that is non-blocking and thread-safe 
for writing, except for collections that are inherently safe, like Array.

A few months ago I did implement a hashed collection (essentially a 
variant of IdentityDictionary) that requires semaphore access for write, 
but not for read. This increases the code complexity somewhat, and the 
code fragility quite a bit. One code change by someone who does not 
understand all of the invariants that must be observed, and it's no 
longer thread-safe. The collection is believed to be thread-safe, but 
testing all of the code branches is very difficult, so it has not been 
thoroughly tested. (Actually, I know of one design bug that I need to 
fix, but I believe I know how to fix it.) This collection is rather 
special-purpose; we had a particular requirement for being able to read 
it even when debugging the process that was writing to it, otherwise I 
would not have bothered.

It's probably possible to write a hashed collection that is thread-safe 
and semaphore-free for both read and write, but unless there's a very 
specific requirement for that it's probably simpler, faster, and more 
reliable to just use a semaphore.

Regards,

-Martin

_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply via email to