Critical section

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In concurrent programming, concurrent accesses to shared resources can lead to unexpected or erroneous behavior. Thus, the parts of the program where the shared resource is accessed need to be protected in ways that avoid the concurrent access. One way to do so is known as a critical section or critical region. This protected section cannot be entered by more than one process or thread at a time; others are suspended until the first leaves the critical section. Typically, the critical section accesses a shared resource, such as a data structure, peripheral device, or network connection, that would not operate correctly in the context of multiple concurrent accesses.

In concurrent programming, concurrent accesses to shared resources can lead to unexpected or erroneous behavior. Thus, the parts of the program where the shared resource is accessed need to be protected in ways that avoid the concurrent access. One way to do so is known as a critical section or critical region. This protected section cannot be entered by more than one process or thread at a time; others are suspended until the first leaves the critical section. Typically, the critical section accesses a shared resource, such as a data structure, peripheral device, or network connection, that would not operate correctly in the context of multiple concurrent accesses. // Process A . . b = x + 5; // instruction executes at time = Tx . Process B: // Process B . . x = 3 + z; // instruction executes at time = Tx . In cases where a locking mechanism with finer granularity is not needed, a critical section is important. In the above case, if A needs to read the updated value of , executing process A, and process B at the same time may not give required results. To prevent this, variable is protected by a critical section. First, B gets the access to the section. Once B finishes writing the value, A gets the access to the critical section, and variable can be read. By carefully controlling which variables are modified inside and outside the critical section, concurrent access to the shared variable is prevented. A critical section is typically used when a multi-threaded program must update multiple related variables without a separate thread making conflicting changes to that data. In a related situation, a critical section may be used to ensure that a shared resource, for example, a printer, can only be accessed by one process at a time.

See also

Database transaction Dekker's algorithm Eisenberg & McGuire algorithm Lamport's bakery algorithm Lock (computer science) Mutual exclusion Peterson's algorithm Szymański's algorithm

References

External links

Critical Section documentation on the Microsoft Docs web page Tutorial on Critical Sections Code examples for Mutex Tutorial on Semaphores Lock contention in Critical Sections

Text derived from https://en.wikipedia.org/w/index.php?oldid=1326089050 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

No external citation was extracted for this revision. That is a statement about the source, not a claim that none exists.