RE: Synchronize mthd

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: JoeWood (joewood_at_discussions.microsoft.com)
Date: 11/19/04


Date: Fri, 19 Nov 2004 11:37:05 -0800

If you check the Rotor code you will see that the implementation is actually
creating an instance of a class derived from ArrayList, passing the parameter
to its constructor.
The derived arraylist (SyncArrayList) is the thread safe version of
ArrayList with thread safe operations used as overrides.
Check the Rotor code for more details:
http://www.dotnet247.com/247reference/system/collections/arraylist/__rotor#synchronized

"AJP" wrote:

> 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
>
>
>
>