Suppose multiple threads execute periodically the DoWork()
method below. Suppose that at some point two threads begin the execution of this method almost simultaneously, so that one of the two local timestamp object is one tick larger than the other.
ICollection collection = // ...
public void DoWork()
{
DateTime timestamp = DateTime.Now;
lock(collection.SyncRoot)
{
// critical section
}
}
If the thread A is characterized by a timestamp equal to t1, while the thread B is characterized by a timestamp t2 equal to t1 + 1 tick, then the thread A will require first the access to the critical section.
How does .NET manage the access to the critical section by multiple threads? Does it put access requests in a queue, so that they are in chronological order? In other words, is the access to the critical section guaranteed according to the order of thread access requests?