#include <wx/thread.h>
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.
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(). | |
bool | TryEnter () |
Try to enter the critical section (same as trying to lock a mutex). | |
void | Leave () |
Leave the critical section allowing other threads use the global data protected by it. | |
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.
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.
bool wxCriticalSection::TryEnter | ( | ) |
Try to enter the critical section (same as trying to lock a mutex).
If it can't, immediately returns false.