JUCE  v5.1.1-3-g1a0b28c73
JUCE API
GenericScopedTryLock< LockType > Class Template Reference

Automatically locks and unlocks a mutex object. More...

#include <juce_ScopedLock.h>

Collaboration diagram for GenericScopedTryLock< LockType >:

Public Member Functions

 GenericScopedTryLock (const LockType &lock) noexcept
 Creates a GenericScopedTryLock. More...
 
 ~GenericScopedTryLock () noexcept
 Destructor. More...
 
bool isLocked () const noexcept
 Returns true if the mutex was successfully locked. More...
 

Private Attributes

const LockType & lock_
 
const bool lockWasSuccessful
 

Detailed Description

template<class LockType>
class GenericScopedTryLock< LockType >

Automatically locks and unlocks a mutex object.

Use one of these as a local variable to provide RAII-based locking of a mutex.

The templated class could be a CriticalSection, SpinLock, or anything else that provides enter() and exit() methods.

e.g.

CriticalSection myCriticalSection;
for (;;)
{
const GenericScopedTryLock<CriticalSection> myScopedTryLock (myCriticalSection);
// Unlike using a ScopedLock, this may fail to actually get the lock, so you
// should test this with the isLocked() method before doing your thread-unsafe
// action..
if (myScopedTryLock.isLocked())
{
...do some stuff...
}
else
{
..our attempt at locking failed because another thread had already locked it..
}
// myCriticalSection gets unlocked here (if it was locked)
}
See also
CriticalSection::tryEnter, GenericScopedLock, GenericScopedUnlock

Constructor & Destructor Documentation

◆ GenericScopedTryLock()

template<class LockType>
GenericScopedTryLock< LockType >::GenericScopedTryLock ( const LockType &  lock)
inlineexplicitnoexcept

Creates a GenericScopedTryLock.

As soon as it is created, this will attempt to acquire the lock, and when the GenericScopedTryLock is deleted, the lock will be released (if the lock was successfully acquired).

Make sure this object is created and deleted by the same thread, otherwise there are no guarantees what will happen! Best just to use it as a local stack object, rather than creating one with the new() operator.

◆ ~GenericScopedTryLock()

template<class LockType>
GenericScopedTryLock< LockType >::~GenericScopedTryLock ( )
inlinenoexcept

Destructor.

The mutex will be unlocked (if it had been successfully locked) when the destructor is called.

Make sure this object is created and deleted by the same thread, otherwise there are no guarantees what will happen!

References GenericScopedLock< LockType >::lock_.

Member Function Documentation

◆ isLocked()

template<class LockType>
bool GenericScopedTryLock< LockType >::isLocked ( ) const
inlinenoexcept

Returns true if the mutex was successfully locked.

Member Data Documentation

◆ lock_

template<class LockType>
const LockType& GenericScopedTryLock< LockType >::lock_
private

◆ lockWasSuccessful

template<class LockType>
const bool GenericScopedTryLock< LockType >::lockWasSuccessful
private

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