Quantcast
Channel: How Critical Section object works exactly for multiple methods - Stack Overflow
Viewing all articles
Browse latest Browse all 3

How Critical Section object works exactly for multiple methods

$
0
0

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 queueLeaveCriticalSection(&cs)}Method2(){EnterCriticalSection(&cs)    //Access common resource (Read/Write) lets say a global queueLeaveCriticalSection(&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 somethingLeaveCriticalSection(&cs)}Method2(){EnterCriticalSection(&cs)    //Do SomethingLeaveCriticalSection(&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.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images