Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which thread will be the first to enter the critical section?

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?

like image 793
enzom83 Avatar asked Sep 05 '12 17:09

enzom83


People also ask

What is critical section in thread?

A critical section is a section of code that needs to be executed without outside interference - i.e. without another thread potentially affecting/being affected by "intermediate" states within the section.

How do you implement a critical section?

Implementation of critical sections A critical section will usually terminate in finite time, and a thread, task, or process will have to wait for a fixed time to enter it (bounded waiting). To ensure exclusive use of critical sections some synchronization mechanism is required at the entry and exit of the program.

Can a thread in critical section be preempted?

1 Answer. Preemption is possible when a process is in its Critical Section. Howerver, any other process which share its Critical Section with the proccess in its CS will have to wait until the process complete its CS.

What is a critical section in Java?

A critical section is a block of code that accesses a shared resource and can't be executed by more than one thread at the same time. To help programmers implement critical sections, Java (and almost all programming languages) offers synchronization mechanisms.


2 Answers

There are absolutely no guarantees on order of threads execution and what thread obtains critical section first.

Note that even priority of a thread will not guarantee the order - different cores/CPUs can execute threads at different priorities at exactly the same time and any thread can reach and obtain critical section first.

Note 2: threads also can be scheduled for execution/wait at arbitrary moments of time, so the fact that 2 different operations in the same thread are next to each other does not mean they will be executed one after another without delay in between. In your case it means that thread A may be stopped as soon as it obtains time-stamp and thread B that is scheduled for execution some time later will easily get later time-stamp but gets to critical section first.

like image 176
Alexei Levenkov Avatar answered Nov 14 '22 23:11

Alexei Levenkov


You are making a tall assumption, one that forever gets programmers in trouble with threads. A thread that obtains the timestamp first is most certainly not guaranteed to enter the lock first as well. Only the odds are high, they are not 100%. Threads get pre-empted by the operating system scheduler. Which can interrupt any thread, including one that just started executing the method call to Monitor.Enter(). Scheduling decisions then may well suspend A and allow B to obtain the lock first.

Nor does it take the scheduler to gum up the order. The core that executes A might not have the "collection" object reference in its data cache, stalling the core long enough while waiting for the memory bus to allow the core that executes B to race ahead. The word "race" is appropriate, making wrong assumptions here causes threading race bugs in your code.

The mechanism behind locks are implemented by the processor, the only entity that can ensure there is no "same time". Every multi-core cpu implements the Compare-and-swap atomic instruction. You can see the version used by .NET in this answer.

like image 29
Hans Passant Avatar answered Nov 14 '22 23:11

Hans Passant