Cgu::Thread::Mutex::Lock Class Reference

A scoped locking class for exception safe Mutex locking. More...

`#include <c++-gtk-utils/mutex.h>`

## Public Member Functions

| Lock (const Mutex::Lock &)=delete
Mutex::Lock & | operator= (const Mutex::Lock &)=delete
int | lock () noexcept
int | trylock () noexcept
int | unlock () noexcept
| Lock (Mutex &mutex_) noexcept
| Lock (Mutex &mutex_, Locked tag) noexcept
| Lock ()=delete
| ~Lock ()

## Friends

class | Cond

## Detailed Description

A scoped locking class for exception safe Mutex locking.

See also
  Thread::Mutex Thread::Mutex::TrackLock Thread::Thread Thread::Cond

## Constructor & Destructor Documentation

## Lock() [1/4]

Cgu::Thread::Mutex::Lock::Lock | ( | const Mutex::Lock & |  | ) |

delete

This class cannot be copied. The copy constructor is deleted.

## Lock() [2/4]

Cgu::Thread::Mutex::Lock::Lock | ( | Mutex & | mutex_ | ) |

inline noexcept

This constructor locks the mutex passed to it. It is not a cancellation point. It does not throw.

Parameters

  mutex_ | The mutex to be locked.

## Lock() [3/4]

Cgu::Thread::Mutex::Lock::Lock | ( | Mutex & | mutex_,
|  | Locked | tag
| ) |  |

inline noexcept

This constructor takes an already locked mutex (say as a result of Mutex::trylock()), and takes ownership of it. It is not a cancellation point. It does not throw.

Parameters

  mutex_ | The mutex to be managed by this object.
  tag | Pass the Cgu::Thread::locked enum tag to this parameter.

## Lock() [4/4]

Cgu::Thread::Mutex::Lock::Lock | ( |  | ) |

delete

This class requires initialisation with a Mutex. The default constructor is deleted.

## ~Lock()

Cgu::Thread::Mutex::Lock::~Lock | ( |  | ) |

inline

The destructor unlocks the owned mutex. It is not a cancellation point. It does not throw.

## Member Function Documentation

## lock()

int Cgu::Thread::Mutex::Lock::lock | ( |  | ) |

inline noexcept

Calls Mutex::lock(), and so locks the mutex and reacquires ownership. It blocks if the mutex is already locked until the mutex becomes free. This method should normally only be called if a previous call has been made to Mutex::Lock::unlock() (that is, where the thread owning the Mutex::Lock object has temporarily allowed another thread to take the mutex concerned). It is not a cancellation point. It does not throw.

Returns
  0 if successful, otherwise the pthread mutex error number.

Note
  With this library implementation, the only pthread error number which could be returned by this method is EDEADLK, which it would do if the default pthread mutex behaviour happens to return that error rather than deadlock in the case of recursive locking. Most default implementations do not do this and hence the return value is usually not worth checking for except during debugging.

See also
  Mutex::TrackLock.

## operator=()

Mutex::Lock& Cgu::Thread::Mutex::Lock::operator= | ( | const Mutex::Lock & |  | ) |

delete

This class cannot be copied. The assignment operator is deleted.

## trylock()

int Cgu::Thread::Mutex::Lock::trylock | ( |  | ) |

inline noexcept

Calls Mutex::trylock(), and so tries to lock the mutex and reacquire ownership, but returns immediately if it is already locked with value EBUSY. This method should normally only be called if a previous call has been made to Mutex::Lock::unlock() (that is, where the thread owning the Mutex::Lock object has temporarily allowed another thread to take the mutex concerned). It is not a cancellation point. It does not throw.

Returns
  0 if successful, otherwise EBUSY.

Note
  With this library implementation, the only pthread error number which could be returned by this method is EBUSY.

See also
  Mutex::TrackLock.

## unlock()

int Cgu::Thread::Mutex::Lock::unlock | ( |  | ) |

inline noexcept

Calls Mutex::unlock(), and so unlocks a locked mutex owned by the calling thread and relinquishes ownership (so temporarily allowing another thread to take the mutex). This method should normally only be called if it is to be followed by a call to Mutex::Lock::lock() or a successful call to Mutex::Lock::trylock() before the Mutex::Lock object concerned goes out of scope (otherwise Mutex::Lock's destructor will attempt to unlock an already unlocked mutex or a mutex of which another thread has by then taken ownership - Mutex::Lock objects do not maintain state). See Mutex::TrackLock::unlock() for a safe version of this method. It is not a cancellation point. It does not throw.

Returns
  0 if successful, otherwise the pthread mutex error number.

Note
  With this library implementation, the only pthread error number which could be returned by this method is EPERM because the calling thread does not own the mutex (however POSIX does not require that return value in that case and hence the return value is usually not worth checking for except during debugging).

See also
  Mutex::TrackLock.

## Friends And Related Function Documentation

## Cond

friend class Cond

friend

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

- mutex.h