I was reading about mutex,semaphores and critical sections. I understand that mutex synchronizes a resource so that only one thread accesses it at a time a semaphore allows a specific no of threads to access a resource but what do critical sections do ??
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.
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++ .
Critical Section: When more than one processes access the same code segment that segment is known as the critical section. The critical section contains shared variables or resources which are needed to be synchronized to maintain the consistency of data variables.
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.
In normal use, a critical section is a section of code that must be executed serially -- i.e., only one thread can execute that code at any given time. You normally accomplish that by protecting the code with a mutex semaphore.
In Windows parlance, a critical section is a data structure (and a few associated functions) that implement at process-specific mutex semaphore (i.e., one that's used only for locking between threads in a single process, not between separate processes).
There are two varieties of semaphores. A mutex semaphore lets only one thread execute at a time. A counted semaphore lets you specify the maximum number of threads that can execute simultaneously. Mutex semaphores are the more common variety, but counted semaphores definitely have uses as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With