Step
up to the Plate.
Problem 2In
the world of computer science, what is a critical section?
Answer:
A critical section is a piece of code
that can only be executed by one thread in order to work correctly.
In reality, a set of data items shared by parallel threads usually
has multiple critical sections. At any point, at most one thread may
be executing one of the critical sections to access the data items.
For instance, consider the following
code segment:
int stack[STACK_SIZE];
unsigned stackIndex;
int pop( void )
{
int result;
result = stack[--stackIndex];
return result;
}
void push(int value)
{
stack[stackIndex++] = value;
}
The contents of both the pop and push
functions should designate critical sections with respect to the array
stack and the unsigned variable stackIndex. This is because preemptive
multitasking may cause two threads to access the stack at the same
time. For instance, two threads may execute --stackIndex and stackIndex++
in parallel, causing confusing symptoms.
2-00
|