Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a critical section?

Just want a little clarity on the this. Imagine I use the windows api of EnterCriticalSection. I call all of them with EnterCriticalSection(&criticalsection);

This is the thread function that is multi threaded

void thread (){

//enter critical section  (part 1)
data
//leave critical section
///more data 1
//entercritical section  (part 2)
//more data 2
//leave critical section 

}

Once a thread enters the critical (part 1), other threads cannot enter that section regardless of whether more data 1 actually has any shared data or not right? Also during that time other threads also cannot enter part 2 of the critical section either.

like image 686
Jake Avatar asked Apr 09 '11 19:04

Jake


People also ask

What do you mean by critical section?

Critical Section refers to the segment of code or the program which tries to access or modify the value of the variables in a shared resource.

What is critical section explain three conditions of critical section?

A critical section is a segment of code which can be accessed by a signal process at a specific point of time. Three must rules which must enforce by critical section are : 1) Mutual Exclusion 2) Process solution 3)Bound waiting.

How do you avoid a critical section?

Race conditions in critical sections can be avoided if the critical section is treated as an atomic instruction. Also, proper thread synchronization using locks or atomic variables can prevent race conditions.


1 Answers

Critical section is a code chunk. If any thread entered it, no other thread can enter until it's free. If 1 and 2 are different critical sections (i.e. handled by a different semaphore), someone can enter 2 if 1 is occupied.

like image 81
iehrlich Avatar answered Sep 24 '22 15:09

iehrlich