[quoted text, click to view] "Gilles" <Gilles@discussions.microsoft.com> wrote in message
news:FAB2BA0D-C49D-4A4E-957B-DEF4E6DD61DC@microsoft.com...
> Yep, and it seems to be the same for Match, Group, TextWriter, TextReader,
> Stack, SortedList & HashTable classes that all return an object of the
> same
> type.
>
> So, if it is not an "official way" (Synchronized fct is not part of an
> interface), it's quite frequent in .NET.
>
Its the only way, really, as I mention below.
[quoted text, click to view] > The only difference is ArrayList's implementation that returns an object
> implementing an interface (IList) which is more rational.
Thats because ArrayList's contract is fully expressible using IList, Queue
and many, many others do not.
[quoted text, click to view] > Otherwise, wouldn't it be more logical to provide directly a
> xxxSynchronized
> derived class rather than providing these wrappers? Is there a recommended
> pattern when you intend to write a class that offers both "light" or
> thread-safe versions?
>
The problem is, you have a variable of type Xxx, and your synced version is
of type XxxSynchronized. Unless XxxSynchronized is derived from Xxx,
XxxSynchronized is pretty close to worthless since you are restricted to
only using a synchronized type in all apis. Therefore you end up with dual
api's, one synced, one not in circumstances where synchronization is not the
API's responsibility, but your own. Too messy, not worth ith.
I would strongly recommend using the same mechanism that the framework uses
when you are doing synchronized wrappers, for synchronized versions you can
simply derive ASynchronized from A and override the methods that need
synchronization.
[quoted text, click to view] > More general question: why not directly a thread-safe version?
> Has anyone already evaluated the cost of systematic use of lock statement?
I can't give you quotes, but it is expensive and the cost would be
prohibitive.