lock used in thread and by event



Hello,

I have a application with two threads. I have also a resoure that is
synchronized by lock.

When testing locks before this App I noticed that if one threaded
application for test purposes

invokes code like below it does not start waiting.

lock(this)

{

..... some code

lock(this)

{

.... some code

} // Execution goes through both locks.

}


Okey for my two threaded Application. In main thread there is a
method that uses lock to protect a data structure. Code is like below:

class MyClass {

public void My Method(object item)
{
lock(_myList)
{
_myList.Remove(item);
}
}

.....

}

In same class there is also a eventhandler to remove items from List
and it's fired from another thread. Code is like below.

class MyClass {
public void OnRemoveItemFromList(object sender, ItemEventArgs e)
{

lock (_myList)
{
_myList.Remove(session)
}

}

......

}



From another thread List item is remove by Event like below:

class SecondThread {
.....

RemoveItemFromListEvent(this, new ItemEventArgs (this));

.....

}



What I have noticed that lock(_myList) does not stop and start waiting
if one of the threads have aquired it.
lock(_myList) goes allways through. So, can anyone explain that is
Event call same as a call executed in thread that owns lock(_myList)
or how this goes?

Can anyone clarify this to me?
Any documentation considering this issue?

Thanks!

.



Relevant Pages

  • RE: Language enhancement idea: C# method instance fields
    ... class MyClass ... Where scope can organize a set of methods with access to particular variables. ... You MUST lock this lock object to access this variable. ... Enumerators, which must own and manage the current position of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ManualResetEvent.WaitOne() and ManualResetEvent.Set()
    ... Regards from Kovan ... > event handlers does not release the lock. ... > class MyClass ... > Kovan Akrei ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Language enhancement idea: C# method instance fields
    ... class MyClass ... Where scope can organize a set of methods with access to particular variables. ... You MUST lock this lock object to access this variable. ... Enumerators, which must own and manage the current position of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading question
    ... I am having trouble deciding on which object i should obtain lock. ... class MyClass ... private object syncRoot in my class and obtain a lock on that? ...
    (microsoft.public.dotnet.framework)
  • Re: synchronization question
    ... Synchronization obtains a lock identified by the ... public void M1 ...
    (comp.lang.java.programmer)

Loading