Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does CCriticalSection do?

Tags:

c++

winapi

What the diffrence between this code:

::EnterCriticalSection( &m_CriticalSection );
//...
::LeaveCriticalSection( &m_CriticalSection );

and the code:

static CCriticalSection cs;
cs.Lock();
//...
cs.UnLock();
like image 874
adir Avatar asked Feb 10 '11 07:02

adir


People also ask

What is critical section in simple words?

The critical section is a code segment where the shared variables can be accessed. An atomic action is required in a critical section i.e. only one process can execute in its critical section at a time. All the other processes have to wait to execute in their critical sections.

What is the important feature of critical section?

The Critical-Section Problem The important feature of the system is that, when one process is executing in its critical section, no other process is to be allowed to execute in its critical section. That is, no two processes are executing in their critical sections at the same time.

What is a critical section and what is the critical section problem?

Informally, a critical section is a code segment that accesses shared variables and has to be executed as an atomic action. The critical section problem refers to the problem of how to ensure that at most one process is executing its critical section at a given time.

What are the rules for 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.


1 Answers

No difference practically. CCriticalSection is the only syntatic sugar of the former. It internally uses EnterCriticalSection and LeaveCriticalSection!

EnterCriticalSection and LeaveCriticalSection are low-level win32 APIs, while CCriticalSection is a MFC class which wraps these functionalities. It has a member data of type CRITICAL_SECTION which is used by the APIs.

MSDN says,

The functionality of the CCriticalSection class is provided by an actual Win32 CRITICAL_SECTION object.

like image 158
Nawaz Avatar answered Nov 15 '22 14:11

Nawaz