G
Guest
I'm trying to figure out how to implement a static Synchronized() method
(like those supported in many of the classes in the Collections namespace).
I'm deriving from CollectionBase for a strongly typed collection. I think I
understand how to use the SyncRoot property to safeguard operations:
lock(this.SyncRoot)
{
// do the operation... add or remove or whatever
}
When I run Object.ReferenceEquals(o1, o2)
//where
o1 = new ArrayList();
// and
02 = ArrayList.Synchronized(o1);
I get false. So there's an entirely new arraylist object created,
presumably with an internal flag saying do only synchronized operations. But
they must share the same internal data structure.
So when I implement Synchronized(o), do I do this....
1. Create a new instance of the CollectionBase-derived class.
2. Set a bool flag saying do only thread-safe operations
3. Copy the IList from the 'o' passed in to the new CollectionBase-derived
class
(like those supported in many of the classes in the Collections namespace).
I'm deriving from CollectionBase for a strongly typed collection. I think I
understand how to use the SyncRoot property to safeguard operations:
lock(this.SyncRoot)
{
// do the operation... add or remove or whatever
}
When I run Object.ReferenceEquals(o1, o2)
//where
o1 = new ArrayList();
// and
02 = ArrayList.Synchronized(o1);
I get false. So there's an entirely new arraylist object created,
presumably with an internal flag saying do only synchronized operations. But
they must share the same internal data structure.
So when I implement Synchronized(o), do I do this....
1. Create a new instance of the CollectionBase-derived class.
2. Set a bool flag saying do only thread-safe operations
3. Copy the IList from the 'o' passed in to the new CollectionBase-derived
class