M
Matt
Hello all,
I have written a collection class that implements IList. The class
currently uses an ArrayList for its internal storage.
I am somewhat confused as to how I should implement the SyncRoot
property on my class. The MSDN documentation seems quite evasive on
the subject:
For collections whose underlying store is not publicly available,
the expected implementation is to return the current instance. Note
that the pointer to the current instance might not be sufficient for
collections that wrap other collections; those should return the
underlying collection's SyncRoot property.
[...]
In the absence of a Synchronized method on a collection, the
expected usage for SyncRoot looks like this:
ICollection MyCollection = ...
lock( MyCollection.SyncRoot ) {
// Some operation on the collection, which is now thread-safe.
}
This begs the question: what if I were to modify my class later, say
to require another object be updated in sync with the ArrayList? I
wouldn't then be able to rely on the ArrayList's SyncRoot, so I would
have to either return the current instance, or have another object to
lock on?
Then, having chosen *what* to return for SyncRoot, do I need to ensure
that every method that could modify my collection first locks on
SyncRoot? e.g.:
lock ( this.SyncRoot )
{
// modify own state
}
It's quite unlikely that this will ever become an issue with my class
due to the way it is used in the application. However, I'd really like
to understand this point (and I hate leaving things half-done
Regards,
Matt
I have written a collection class that implements IList. The class
currently uses an ArrayList for its internal storage.
I am somewhat confused as to how I should implement the SyncRoot
property on my class. The MSDN documentation seems quite evasive on
the subject:
For collections whose underlying store is not publicly available,
the expected implementation is to return the current instance. Note
that the pointer to the current instance might not be sufficient for
collections that wrap other collections; those should return the
underlying collection's SyncRoot property.
[...]
In the absence of a Synchronized method on a collection, the
expected usage for SyncRoot looks like this:
ICollection MyCollection = ...
lock( MyCollection.SyncRoot ) {
// Some operation on the collection, which is now thread-safe.
}
This begs the question: what if I were to modify my class later, say
to require another object be updated in sync with the ArrayList? I
wouldn't then be able to rely on the ArrayList's SyncRoot, so I would
have to either return the current instance, or have another object to
lock on?
Then, having chosen *what* to return for SyncRoot, do I need to ensure
that every method that could modify my collection first locks on
SyncRoot? e.g.:
lock ( this.SyncRoot )
{
// modify own state
}
It's quite unlikely that this will ever become an issue with my class
due to the way it is used in the application. However, I'd really like
to understand this point (and I hate leaving things half-done
Regards,
Matt