I have a question about locks because I am seeing behavior that does not match what I though was the correct behavior. I thought when you put a lock around code the first instance that ran it would of course get access and then each access after that (while the first instance is still executing the code) would be essentially queued up on that lock waiting for the first to exit. But what I am seeing is that the last one there is the first to get access to the code... which doesn't make sense. If someone can shed some light I would appreciate it. Below is an example of the code and the data that it returned you can see that 4 lines that were logged before the lock and the last one was the first to go inside the lock????
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ======= DATE
5/11/2005 11:26:43 AM - Data After Lock: ======= DATE
5/11/2005 11:26:43 AM - Data Before Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
//lockingObject is a private object contained in the class with this method
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ======= DATE
5/11/2005 11:26:43 AM - Data After Lock: ======= DATE
5/11/2005 11:26:43 AM - Data Before Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
//lockingObject is a private object contained in the class with this method
Code:
public void getIncomingData(object data)
{
try
{
myLog.Log(string.Format("Data Before Lock: {0}",data),true);
lock(lockingObject)
{
myLog.Log(string.Format("Data After Lock: {0}",data),true);
myParser.getIncomingData((string)data);
}
}
catch(Exception e)
{
myLog.Log(string.Format("Error in DataCapture getIncomingData: {0}",e.Message));
}
}