Open CASCADE Technology 7.8.2.dev
Standard_Mutex Class Reference

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

#include <Standard_Mutex.hxx>

Inheritance diagram for Standard_Mutex:

Data Structures

class  Sentry
 Simple sentry class providing convenient interface to mutex. More...
 

Public Member Functions

 Standard_Mutex ()
 Constructor: creates a mutex object and initializes it. It is strongly recommended that mutexes were created as static objects whenever possible.
 
 ~Standard_Mutex ()
 Destructor: destroys the mutex object.
 
void Lock ()
 Method to lock the mutex; waits until the mutex is released by other threads, locks it and then returns.
 
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.
 
void Unlock ()
 Method to unlock the mutex; releases it to other users.
 
- Public Member Functions inherited from Standard_ErrorHandler::Callback
void RegisterCallback ()
 Registers this callback object in the current error handler (if found).
 
void UnregisterCallback ()
 Unregisters this callback object from the error handler.
 
virtual ~Callback ()
 Destructor.
 

Additional Inherited Members

- Protected Member Functions inherited from Standard_ErrorHandler::Callback
 Callback ()
 Empty constructor.
 

Detailed Description

Mutex: a class to synchronize access to shared data.

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

Current implementation is very simple and straightforward; it is just a wrapper around POSIX pthread library on UNIX/Linux, and CRITICAL_SECTIONs on Windows NT. It does not provide any advanced functionality such as recursive calls to the same mutex from within one thread (such call will freeze 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_Mutex()

Standard_Mutex::Standard_Mutex ( )

Constructor: creates a mutex object and initializes it. It is strongly recommended that mutexes were created as static objects whenever possible.

◆ ~Standard_Mutex()

Standard_Mutex::~Standard_Mutex ( )

Destructor: destroys the mutex object.

Member Function Documentation

◆ Lock()

void Standard_Mutex::Lock ( )

Method to lock the mutex; waits until the mutex is released by other threads, locks it and then returns.

◆ TryLock()

Standard_Boolean Standard_Mutex::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.

◆ Unlock()

void Standard_Mutex::Unlock ( )
inline

Method to unlock the mutex; releases it to other users.


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