How Critical Section object works exactly for multiple methods

1.8k views Asked by At

CASE I:

Scenario : I have two different methods, each sharing common global resource. Method1() is accessed by ThreadA and Method2() by many other Threads but not ThreadA.

Requirement :What I require is if ThreadA is accessing Method1() no other thread access Method2().

Solution :So using a common critical section object will prevent any conflict in global resource. For eg.

Method1()
{
EnterCriticalSection(&cs)
    //Access common resource (Read/Write) lets say a global queue
LeaveCriticalSection(&cs)
}

Method2()
{
EnterCriticalSection(&cs)
    //Access common resource (Read/Write) lets say a global queue
LeaveCriticalSection(&cs)
}

CASE II:

Scenario :I have two different methods, and they do not share any resource.

Requirement :What I require is different threads may not run Method1() simultaneously. Similarly for Method2(). But there is no problem if any of the threads run both the methods at the same time. For eg. ThreadA and ThreadB both may not access Method1() at the same time but it should be possible that ThreadA is running Method1() and ThreadB is running Method2() simultaneously. In this case if I use same critical section object in both the methods, then lets say ThreadA runs method1, ThreadB will have to wait for ThreadA to Leave critical section before it starts executing Method2.

Method1()
{
EnterCriticalSection(&cs)
    //Do something
LeaveCriticalSection(&cs)
}

Method2()
{
EnterCriticalSection(&cs)
    //Do Something
LeaveCriticalSection(&cs)
}

Solution :So in this case do we use different critical section object for different Methods?? Am I correct in bth the cases?

I am not clear with the concept of critical section object when dealing multiple functions. Please help me in understanding the concept. I tried online but could not find relevant source that could clear my doubts.

2

There are 2 answers

0
MSalters On BEST ANSWER

Critical sections protect resources, by blocking threads from running specific parts of your code.

For that reason, each resource has an associated critical section, but multiple pieces of code that all access the same resource must also use the same critical section object.

It is technically OK for a single critical section to protect two resources. This can actually make sense if the two objects are always used together. In fact, you could run your program with just one Critical Section for everything. Not efficient, but not unheard of either. For instance, Python uses that mechanism (Global Interpreter Lock)

But if they're two independent objects, using individual critical sections allows concurrent use of the objects. This is far more efficient. The downside is that if the two objects are sometimes used together, you should always enter their critical sections in the same order.

0
ravi On

Your both assumptions are correct as per requirements.

Basically critical section is a code where you want only single thread to enter. Till that thread finishes no other thread is allowed to enter into that critical section.And that critical section could contain multiple functions as well.

You hold mutexes at the begining of critical sections which you release after leaving that section.