LMMS
Loading...
Searching...
No Matches
juce::ScopedTryWriteLock Class Reference

#include <juce_ScopedWriteLock.h>

Public Member Functions

 ScopedTryWriteLock (ReadWriteLock &lockIn) noexcept
 ScopedTryWriteLock (ReadWriteLock &lockIn, bool acquireLockOnInitialisation) noexcept
 ~ScopedTryWriteLock () noexcept
bool isLocked () const noexcept
bool retryLock () noexcept

Private Attributes

ReadWriteLocklock
bool lockWasSuccessful

Detailed Description

Automatically locks and unlocks a ReadWriteLock object.

Use one of these as a local variable to control access to a ReadWriteLock.

e.g.

for (;;)
{
const ScopedTryWriteLock myScopedTryLock (myLock);
// Unlike using a ScopedWriteLock, 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 some other thread has already locked the object..
}
// myLock gets unlocked here (if it was locked).
}
Definition juce_ReadWriteLock.h:48
ScopedTryWriteLock(ReadWriteLock &lockIn) noexcept
Definition juce_ScopedWriteLock.h:131
bool has(T &t, Z &z)
Definition ports.cpp:334
See also
ReadWriteLock, ScopedTryWriteLock

@tags{Core}

Constructor & Destructor Documentation

◆ ScopedTryWriteLock() [1/2]

juce::ScopedTryWriteLock::ScopedTryWriteLock ( ReadWriteLock & lockIn)
inlinenoexcept

Creates a ScopedTryWriteLock and calls ReadWriteLock::tryEnterWrite() immediately. When the ScopedTryWriteLock object is destructed, the ReadWriteLock will be unlocked (if it was successfully acquired).

Make sure this object is created and destructed 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.

◆ ScopedTryWriteLock() [2/2]

juce::ScopedTryWriteLock::ScopedTryWriteLock ( ReadWriteLock & lockIn,
bool acquireLockOnInitialisation )
inlinenoexcept

Creates a ScopedTryWriteLock.

If acquireLockOnInitialisation is true then as soon as it is created, this will call ReadWriteLock::tryEnterWrite(), and when the ScopedTryWriteLock object is destructed, the ReadWriteLock will be unlocked (if it was successfully acquired).

Make sure this object is created and destructed 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.

◆ ~ScopedTryWriteLock()

juce::ScopedTryWriteLock::~ScopedTryWriteLock ( )
inlinenoexcept

Destructor.

The ReadWriteLock's exitWrite() method will be called when the destructor is called.

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

Member Function Documentation

◆ isLocked()

bool juce::ScopedTryWriteLock::isLocked ( ) const
inlinenoexcept

Returns true if the mutex was successfully locked.

◆ retryLock()

bool juce::ScopedTryWriteLock::retryLock ( )
inlinenoexcept

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

Member Data Documentation

◆ lock

ReadWriteLock& juce::ScopedTryWriteLock::lock
private

◆ lockWasSuccessful

bool juce::ScopedTryWriteLock::lockWasSuccessful
private

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