Collection and synchronisation.

  • Thread starter Thread starter wesley
  • Start date Start date
W

wesley

Hi List,

I have a question on collection synchronisation. For example I have an
ArrayList called myList and the code is like this:

lock (myList.SyncRoot)
{
// do update on myList
Monitor.PulseAll(myList.SyncRoot);
}

Question, do I need to do Monitor.PulseAll() in here? Because the code above
is potentially accessed by several thread.

Thanks,

Wes
 
Not sure what your doing, but you don't need pulse here. All other threads
will block on lock (if they lock the same syncRoot instance) and be release
in turn as lock is released. hth
 
Thanks,

I got confused a bit with the Monitor.Pulse/PulseAll doc because it says
here:
"The thread that currently owns the lock on the specified object invokes
this method to signal all threads waiting to acquire the lock on the object.
After the signal is sent, the waiting threads are moved to the ready queue.
When the thread that invoked PulseAll releases the lock, the next thread in
the ready queue acquires the lock."

So I thought I need to do Pulse so the other thread waiting can try to get
the lock.

Thanks,

wes
 
That is when your doing fancier stuff with monitor and waits, etc (i.e.
readers and writers). Normally you can just use "lock()" for many things.
lock actualy is sugar for Monitor.
 
Back
Top