Difference between "Critical Section", "Critical Region" and "Constrained Execution Region"
Asked Answered
C

8

16

Are these actually three different concepts or am I getting jumbled? (I've been reading articles about threading and garbage collection together and have confused myself.)

"Critical section" - I think this may just be the term for sections of code that you don't want multiple threads accessing at the same time i.e. inside lock and Monitor.Enter/Exit statements?

"Critical region" - No real clue here - MSDN says something along the lines of "This tells a host that exceptions thrown within the section might have wider effect". And that "hosts of the CLR e.g. Sql Server" may choose to handle exceptions thrown within critical regions "differently". Differently how? And why? And, most importantly, in what real world scenarios might I need to mark code as a critical region?

"Constrained Execution Region" - I came across this when reading about the CriticalFinalizerObject in a garbage collection article.

All I can understand from MSDN on this one is that code within one of these regions is somehow guaranteed to run (but how?) and must therefore not throw "out-of-band" exceptions.

What is an out-of-band exception? (I did google this but it just asked me if I meant "out of bounds exception").

Is it any unhandled exception? Or only certain types of exception? And again, most importantly, in what real world scenarios might I need a "constrained execution region"?

As I don't understand the concepts at all well, I'm not sure what tags this question needs other than ".NET".

Corenecoreopsis answered 14/4, 2009 at 13:24 Comment(2)
I'm pretty sure your understanding of Critical Section is correct.Ebracteate
That's a start :). I'm not sure if I've used the correct terms. I've seen "Critical Section" in articles about locking. I've only seen "Critical Region" in the MSDN page for Thread.BeginCriticalRegion. I know "Constrained Execution Region" is because there's an MSDN page for it.Corenecoreopsis
S
7

Just my understanding of these concepts:

Critical section - as you said.

Critical region - This seems the big-picture version of "don't let exceptions escape from a thread".

Constrained Execution Region - This is a way to make a piece of code more or less atomic by guarding against interruption by exceptions. The example on this page uses it to make sure that the allocation and storing of a handle are both executed. Note that there is no roll-back, it's more of a preventive system.

There are guidelines for "normal programming" that look a little like this, ie when overriding Equals or an implicit operator you should not throw (anything).

Suffering answered 14/4, 2009 at 13:58 Comment(2)
Sorry for the delay in thanks. +1 and I'm accepting this for the description and link to example combo. I was really hoping for a bit of detail on the "Critical Region" and what exactly an out-of-band exception is but I probably shouldn't have asked multiple questions.Corenecoreopsis
An out-of-band exception is an exception that isn't thrown by the code that is currently directly being executed, but by part of the framework. This term encompasses the following exceptions: StackOverflowException, OutOfMemoryException and ThreadAbortException.Disconnected
S
3

According to Concurrent Programming on Windows by Joe Duffy the definitions for critical section/region are as follows:

Critical section: In Win32 critical section is a simple data structure (CRITICAL_SECTION) used to build critical regions.

Critical region: is a code region that enjoys mutual exclusion (this seems to be what you're referring to as a critical section in the above).

Sayre answered 14/4, 2009 at 13:49 Comment(1)
Thanks for the response. "Critical Section" came from msdn.microsoft.com/en-us/library/c5kehkcz.aspx. And "Critical Region" from msdn.microsoft.com/en-us/library/…. Maybe "Critical Region" as I listed it is not an actual concept?Corenecoreopsis
A
3

This blog entry will fill in your confusion. Link

Aramenta answered 10/11, 2010 at 2:17 Comment(0)
I
2

Microsoft could be defining things differently, but in general, critical region and critical section are the same thing.

They are used to describe regions where two or more processes (or threads) are accessing the same shared memory, and failure to coordinate them will lead to race conditions that prevent the desired behavior in execution, such as the classic example that Pétur gives for incrementing and decrementing a variable above.

A nice discussion can be found in Tanenbaum, A. S., and Bos, H. (2015). Modern Operating Systems (Pearson, Boston, Ma), 4th ed, pp. 1101. Earlier editions as well as the course web sites of numerous universities that offer materials for their operating systems courses.

Intertwine answered 4/1, 2016 at 0:56 Comment(0)
D
1

Critical Section A set of instructions, some of which access shared objects.

Ignore for the moment that both ProcessA and ProcessB can be executed concurrently. You would not want to depend on the value of x, if either ProcessA or ProcessB are to be executed concurrently by many different threads as it will most likely lead to a race condition.

In this example, the two while loops are Critical Sections.

int x = 0;
Process A() {
    while(true) {
        x++;
        x--;
    }
}
Process B() {
    while(true) {
        x++;
        x--;
    }
}

Critical Region A set of critical sections.

Now assume that you can execute ProcessA and ProcessB concurrently. Each process has a critical section. Both of the sections share the same variable (x). Together, the two critical sections form a critical region. Why is this important? If you assume that the critical section of ProcessA is guarded by mutual exclusion, you will still get incorrect results in x as ProcessB does not honour the mutual exclusion. You need to implement mutual exclusion on the critical region, by implementing it on every critical section which makes up the region.

Duaneduarchy answered 23/10, 2014 at 16:18 Comment(0)
D
0

In Windows, I've only worked with a Critical Section so far. To my experience it is a Win32 userspace thread-locking mechanism. This means it can be used within one process to lock resources shared by multiple threads. This is not system-wide, only process-wide. An example of what it is not is a kernel-space lock (like mutexes).

For example, boost::thread uses critical sections in its Win32 implementation -- or at least it did when I used it -- and uses mutexes in Linux, via pthreads.

Duotone answered 14/4, 2009 at 16:36 Comment(1)
Thanks for the response. I don't fully understand it e.g. I have not heard of userspace, boost::thread, kernel-space or pthread. I'm just looking those up. I am gathering that these terms must have different meanings in Win32 to .NET and that's fine as I want to improve my understanding of both.Corenecoreopsis
E
0

Any section of code that should be finished by each process that begins it before another process can enter it is called a critical region.

Elenor answered 25/5, 2009 at 8:48 Comment(0)
G
0

Critical section: is a aegment of code. Every process has a critical section in which exchanging comon variables, updating table, writing file and... The important feature is that, when one process is executing in its critical section, no other process is allowed to enter its critical section and provides manual exclusion. Of course, to achieve that, a synchronization tool called "semaphore" is used when there arw 'n' processes. A semaphore is an integer varaible.

Critical region: is a synchronization construct which guards against certain simple errors associated with semaphore solution to the critical section problem that may made by the programmer.

Ghirlandaio answered 17/3, 2014 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.