On Fri, 25 May 2007 12:08:06 -0700, <freefighter@abv.bgwrote:
Quote:
so according to my knowlegde, the lock() mechanism is locking the code
in the {} that follow it, so in this case
its about copying references to an instance of the storage object.
Yes, you have that correct.
Quote:
to if one thread is using the object by already set variable. so locking
just ensures that references to the object are done one by the time,
but after the pointer is returned, the lock is no longer in charge of
how the reference is used ..
Yes, you're right. The code you posted doesn't keep the object locked
once the reference has been returned.
Quote:
in the example (up) if the Sort() method takes time, what happens if
method 2 tries to Add() asyncronously ??! ;)
Something bad, I'm sure. :)
There are a variety of ways to deal with the issue, all of which at the
core require you to ensure that you only have one thread accessing your
m_SomeList object at time. If there were a way to ensure that only one
reference to it existed at any given time, that would do the job, but
there's not really any way to do that. So instead you have to make sure
that operations on the object are synchronized with any other operation on
the object. ArrayList doesn't do this for you, so you have to do it
yourself.
As a start, allowing the object reference itself to escape your
StorageType class is just asking for trouble, because anyone can write
code that gets that reference does something to it. If you know the
object is going to be used from multiple threads, you need a single
"gatekeeper" that ensures that operations on the object are done in a
synchronized way. The suggestion Nicholas provided is one way to do this.
Pete