U
uupi_duu
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)
}
}
......
}
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!
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!