What is a critical section in streaming?

Please, can someone briefly tell me an example, what does the critical section tool mean? in plain language

+4
source share
4 answers

A critical section is a section of code that must be executed without external interference — that is, without another thread that potentially affects / is affected by the “intermediate” states within the section.

For example, a backup system may have a critical section when backing up something in that it needs to check if this item is available, and then mark it as no longer available, without any other attempt to reserve a room that changes this status in the middle.

Thus, the critical part of the code snippet is where only one thread of execution is allowed to prevent things like race conditions.

+4
source

A critical section is a section of code in which, if more than one thread was doing this at the same time, they could interfere with each other, so as to cause an incorrect result or another malfunction. Imagine a simple banking procedure to process 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 that 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 the ace programmer DonaldJavaSlump adds threads, because it’s 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 second processing code section:

 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 is nearing completion of its work:

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

Now John and Mrs. John are happy again and embark on a free money cruise.

+1
source

A critical section wraps a portion of your code in which shared data is modified. The monitor ensures that only one thread at a time.

0
source

The critical section procedure is an approach to the problem of two or more programs competing for the same resource at the same time. Imagine that two programs want to increase the counter. If both do this at the same time: extract the operand, increase it and save the increased value, then one of the increments will be lost. On modern processors, programs can use atomic read-modify-write instructions, such as fetch-and-op, compare-and-swap, or exchange. On early processors, these instructions did not exist; the problem was to perform the increment atomically using only normal assembler instructions. The problem was identified and first resolved by Edsgar Dijkstra. The “critical section” is its name for the code that solved the problem.

0
source

Source: https://habr.com/ru/post/1304644/


All Articles