| Version: 3.2.5

Public Member Functions | List of all members
wxCriticalSection Class Reference
Class List by Category » 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. More...
| ~wxCriticalSection ()
| Destructor frees the resources. More...
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(). More...
bool | TryEnter ()
| Try to enter the critical section (same as trying to lock a mutex). More...
void | Leave ()
| Leave the critical section allowing other threads use the global data protected by it. More...

## Constructor & Destructor Documentation

## ◆ wxCriticalSection()

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

Default constructor initializes critical section object.

By default critical sections are recursive under Unix and Windows.

## ◆ ~wxCriticalSection()

wxCriticalSection::~wxCriticalSection | ( |  | ) |

Destructor frees the resources.

## Member Function Documentation

## ◆ Enter()

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.

## ◆ Leave()

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.

## ◆ TryEnter()

bool wxCriticalSection::TryEnter | ( |  | ) |

Try to enter the critical section (same as trying to lock a mutex).

If it can't, immediately returns false.

Since
  2.9.3