Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is critical section in threading?

Please can some one briefly tell me with example what does the means of critical section? in simple language

like image 620
Red Swan Avatar asked Mar 20 '10 12:03

Red Swan


People also ask

What is meant by critical section?

The critical section is a code segment where the shared variables can be accessed. An atomic action is required in a critical section i.e. only one process can execute in its critical section at a time. All the other processes have to wait to execute in their critical sections.

What is critical section in synchronization?

To ensure exclusive use of critical sections some synchronization mechanism is required at the entry and exit of the program. Critical section is a piece of a program that requires mutual exclusion of access.

What is the use of critical section?

The critical section refers to the segment of code where processes access shared resources, such as common variables and files, and perform write operations on them. Since processes execute concurrently, any process can be interrupted mid-execution.

What is a critical section describe an example?

Critical sections are sequences of instructions that cannot be interleaved among multiple threads. A simple example of a critical section arises when two threads share a global variable globalvar and both try to change its value with globalvar++ .


2 Answers

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.

For instance, a reservation system might have a critical section when reserving something in that it needs to both check to see if the item is available and then mark it as no longer available, without some other attempt at reserving the room changing that status in the middle.

Thus, the critical section of a piece of code is a place where only one thread of execution is allowed to be at a time, to prevent things like race conditions.

like image 54
Amber Avatar answered Oct 20 '22 00:10

Amber


A critical section is a section of code where if more than one thread was to be doing it an the same time, they could interfere with each other is such a way as to cause an incorrect result or other malfunction. Imagine a simple bank routine for processing a check:

doCheck (writer, recipient, amount) {
    if (amount < 0) return DIAF;
    balance = getBalance( writer );
    if (balance < amount) return NSF;
    setBalance( recipient, getBalance(recipient) + amount );
    setBalance( writer, balance - amount );
}

Now imagine my balance is $11,000 and I wrote two checks:

John, ColumbiaHouse, $0.01
John, MrsJohn, $10,000

Now our bank processes so many checks that "ace" programmer DonaldJavaSlump adds threading, because performance -- but he doesn't know about critical sections, so thread 1 starts processing the first check:

if (amount < 0) return DIAF;             // (0.01 < 0)?  OK
balance = getBalance(John)               // $11,000
if (balance < amount) return NSF;        // (11,000 < 0.01?)  OK
setBalance( ColumbiaHouse, ...           // KaChing! ColumbiaHouse gets paid

and now thread 2 starts through the same section of code processing the second check:

if (amount < 0) return DIAF;             // (10,000 < 0)?  OK
balance = getBalance( John );            // $11,000 (still!)
if (balance < amount) return NSF;        // (11,000 < 10,000?)  OK
setBalance( MrsJohn, ...                 // KaChing! MrsJohn gets paid
setBalance( John, balance - amount );    // my balance is now $1,000 :(

and then thread 1 gets around to finishing its job:

setBalance( John, balance - amount );    // now balance is $10,999.99!!! :)

Now John and MrsJohn are extra happy and go on a cruise with free money!!

like image 35
John Hascall Avatar answered Oct 19 '22 23:10

John Hascall