What the diffrence between this code:
::EnterCriticalSection( &m_CriticalSection );
//...
::LeaveCriticalSection( &m_CriticalSection );
and the code:
static CCriticalSection cs;
cs.Lock();
//...
cs.UnLock();
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With