# FoundationClasses
TKernel
Standard

# Standard_Mutex Class Reference

Mutex: a class to synchronize access to shared data. More...

`#include <Standard_Mutex.hxx>`

Inheritance diagram for Standard_Mutex:

[legend]

## Public Member Functions

Standard_EXPORT | Standard_Mutex ()
| Constructor: creates a mutex object and initializes it. It is strongly recommended that mutexes were created as static objects whenever possible.
Standard_EXPORT | ~Standard_Mutex ()
| Destructor: destroys the mutex object.
Standard_EXPORT void | Lock ()
| Method to lock the mutex; waits until the mutex is released by other threads, locks it and then returns.
Standard_EXPORT Standard_Boolean | TryLock ()
| Method to test the mutex; if the mutex is not hold by other thread, locks it and returns True; otherwise returns False without waiting mutex to be released.
Standard_EXPORT void | Unlock ()
| Method to unlock the mutex; releases it to other users.

## Private Member Functions

virtual void | DestroyCallback ()
| Callback method to unlock the mutex if OCC exception or signal is raised.

## Private Attributes

pthread_mutex_t | myMutex

## Data Structures

class | Sentry
| Simple sentry class providing convenient interface to mutex. More...
class | SentryNested
| Advanced Sentry class providing convenient interface to manipulate a mutex from one thread. More...

## Detailed Description

This is simple encapsulation of tools provided by the operating system to syncronize access to shared data from threads within one process.

Current implementation is very simple and straightforward; it is just a wrapper around POSIX pthread librray on UNIX/Linux, and CRITICAL_SECTIONs on Windows NT. It does not provide any advanced functionaly such as recursive calls to the same mutex from within one thread (such call will froze the execution).

Note that all the methods of that class are made inline, in order to keep maximal performance. This means that a library using the mutex might need to be linked to threads library directly.

The typical use of this class should be as follows:

- create instance of the class Standard_Mutex in the global scope (whenever possible, or as a field of your class)
- create instance of class Standard_Mutex::Sentry using that Mutex when entering critical section

Note that this class provides one feature specific to Open CASCADE: safe unlocking the mutex when signal is raised and converted to OCC exceptions (Note that with current implementation of this functionality on UNIX and Linux, C longjumps are used for that, thus destructors of classes are not called automatically).

To use this feature, call RegisterCallback() after Lock() or successful TryLock(), and UnregisterCallback() before Unlock() (or use Sentry classes).

## Constructor & Destructor Documentation

Standard_EXPORT Standard_Mutex::Standard_Mutex | ( |  | ) |

Standard_EXPORT Standard_Mutex::~Standard_Mutex | ( |  | ) |

## Member Function Documentation

virtual void Standard_Mutex::DestroyCallback | ( |  | ) | `[private, virtual]`

| Implements Standard_ErrorHandlerCallback.

Standard_EXPORT void Standard_Mutex::Lock | ( |  | ) |

Standard_EXPORT Standard_Boolean Standard_Mutex::TryLock | ( |  | ) |

void Standard_Mutex::Unlock | ( |  | ) | `[inline]`

## Field Documentation

pthread_mutex_t Standard_Mutex::myMutex `[private]`

The documentation for this class was generated from the following file:

- Standard_Mutex.hxx