# wxCriticalSection Class Reference
[Threading]

`#include <wx/thread.h>`

## Detailed Description

A critical section object is used for exactly the same purpose as a wxMutex.

The only difference is that under Windows platform critical sections are only visible inside one process, while mutexes may be shared among processes, so using critical sections is slightly more efficient.

The terminology is also slightly different: mutex may be locked (or acquired) and unlocked (or released) while critical section is entered and left by the program.

Finally, you should try to use wxCriticalSectionLocker class whenever possible instead of directly using wxCriticalSection for the same reasons wxMutexLocker is preferable to wxMutex - please see wxMutex for an example.

##

Library: wxBase

Category: Threading

Note:
  Critical sections can be used before the wxWidgets library is fully initialized. In particular, it's safe to create global wxCriticalSection instances.

See also:
  wxThread, wxCondition, wxCriticalSectionLocker

## Public Member Functions
| wxCriticalSection (wxCriticalSectionType critSecType=wxCRITSEC_DEFAULT)
| Default constructor initializes critical section object.
| ~wxCriticalSection ()
| Destructor frees the resources.
void | Enter ()
| Enter the critical section (same as locking a mutex): if another thread has already entered it, this call will block until the other thread calls Leave().
void | Leave ()
| Leave the critical section allowing other threads use the global data protected by it.

List of all members.

## Constructor & Destructor Documentation

wxCriticalSection::wxCriticalSection | ( | wxCriticalSectionType | critSecType = `wxCRITSEC_DEFAULT` | ) |

Default constructor initializes critical section object.

By default critical sections are recursive under Unix and Windows.

wxCriticalSection::~wxCriticalSection | ( |  | ) |

Destructor frees the resources.

## Member Function Documentation

void wxCriticalSection::Enter | ( |  | ) |

Enter the critical section (same as locking a mutex): if another thread has already entered it, this call will block until the other thread calls Leave().

There is no error return for this function.

After entering the critical section protecting a data variable, the thread running inside the critical section may safely use/modify it.

Note that entering the same critical section twice or more from the same thread doesn't result in a deadlock; in this case in fact this function will immediately return.

void wxCriticalSection::Leave | ( |  | ) |

Leave the critical section allowing other threads use the global data protected by it.

There is no error return for this function.

|  | [ top ]