Loading...
Searching...
No Matches
juce::GenericScopedTryLock< LockType > Class Template Reference

## Detailed Description

template<class LockType>
class juce::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)
}
juce::CriticalSection
A re-entrant mutex.
Definition juce_CriticalSection.h:55
juce::GenericScopedTryLock::GenericScopedTryLock
GenericScopedTryLock(const LockType &lock, bool acquireLockOnInitialisation=true) noexcept
Creates a GenericScopedTryLock.
Definition juce_ScopedLock.h:229

See also
  CriticalSection::tryEnter, GenericScopedLock, GenericScopedUnlock

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

- juce_core/threads/juce_ScopedLock.h

## Public Member Functions

| GenericScopedTryLock (const LockType &lock, bool acquireLockOnInitialisation=true) noexcept
| Creates a GenericScopedTryLock.
| ~GenericScopedTryLock () noexcept
| Destructor.
bool | isLocked () const noexcept
| Returns true if the mutex was successfully locked.
bool | retryLock () const noexcept
| Retry gaining the lock by calling tryEnter on the underlying lock.

## Constructors and Destructors

## GenericScopedTryLock()

template<class LockType>

juce::GenericScopedTryLock< LockType >::GenericScopedTryLock | ( | const LockType & | lock,
|  | bool | acquireLockOnInitialisation = true )

inline explicit noexcept

Creates a GenericScopedTryLock.

If acquireLockOnInitialisation is true then as soon as this ScopedTryLock is created, it will attempt to acquire the lock with tryEnter.

You can retry acquiring the lock by calling retryLock.

When 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.

See also
  retryLock, isLocked

## ~GenericScopedTryLock()

template<class LockType>

juce::GenericScopedTryLock< LockType >::~GenericScopedTryLock | ( |  | ) |

inline noexcept

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!

## Member Functions

## isLocked()

template<class LockType>

bool juce::GenericScopedTryLock< LockType >::isLocked | ( |  | ) | const

inline noexcept

Returns true if the mutex was successfully locked.

Referenced by juce::WaitFreeListeners< Listener >::call(), and juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding().

## retryLock()

template<class LockType>

bool juce::GenericScopedTryLock< LockType >::retryLock | ( |  | ) | const

inline noexcept

Retry gaining the lock by calling tryEnter on the underlying lock.

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited