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

#include <juce_ReadWriteLock.h>

Classes

struct  ThreadRecursionCount

Public Member Functions

 ReadWriteLock () noexcept
 ~ReadWriteLock () noexcept
void enterRead () const noexcept
bool tryEnterRead () const noexcept
void exitRead () const noexcept
void enterWrite () const noexcept
bool tryEnterWrite () const noexcept
void exitWrite () const noexcept

Private Member Functions

bool tryEnterWriteInternal (Thread::ThreadID) const noexcept

Private Attributes

SpinLock accessLock
WaitableEvent readWaitEvent
WaitableEvent writeWaitEvent
int numWaitingWriters = 0
int numWriters = 0
Thread::ThreadID writerThreadId = {}
Array< ThreadRecursionCountreaderThreads

Detailed Description

A critical section that allows multiple simultaneous readers.

Features of this type of lock are:

  • Multiple readers can hold the lock at the same time, but only one writer can hold it at once.
  • Writers trying to gain the lock will be blocked until all readers and writers have released it
  • Readers trying to gain the lock while a writer is waiting to acquire it will be blocked until the writer has obtained and released it
  • If a thread already has a read lock and tries to obtain a write lock, it will succeed if there are no other readers
  • If a thread already has the write lock and tries to obtain a read lock, this will succeed.
  • Recursive locking is supported.
See also
ScopedReadLock, ScopedWriteLock, CriticalSection

@tags{Core}

Constructor & Destructor Documentation

◆ ReadWriteLock()

juce::ReadWriteLock::ReadWriteLock ( )
noexcept

Creates a ReadWriteLock object.

◆ ~ReadWriteLock()

juce::ReadWriteLock::~ReadWriteLock ( )
noexcept

Destructor. If the object is deleted whilst locked, any subsequent behaviour is undefined.

Member Function Documentation

◆ enterRead()

void juce::ReadWriteLock::enterRead ( ) const
noexcept

Locks this object for reading.

Multiple threads can simultaneously lock the object for reading, but if another thread has it locked for writing, then this will block until it releases the lock.

See also
exitRead, ScopedReadLock

◆ enterWrite()

void juce::ReadWriteLock::enterWrite ( ) const
noexcept

Locks this object for writing.

This will block until any other threads that have it locked for reading or writing have released their lock.

See also
exitWrite, ScopedWriteLock

◆ exitRead()

void juce::ReadWriteLock::exitRead ( ) const
noexcept

Releases the read-lock.

If the caller thread hasn't got the lock, this can have unpredictable results.

If the enterRead() method has been called multiple times by the thread, each call must be matched by a call to exitRead() before other threads will be allowed to take over the lock.

See also
enterRead, ScopedReadLock

◆ exitWrite()

void juce::ReadWriteLock::exitWrite ( ) const
noexcept

Releases the write-lock.

If the caller thread hasn't got the lock, this can have unpredictable results.

If the enterWrite() method has been called multiple times by the thread, each call must be matched by a call to exit() before other threads will be allowed to take over the lock.

See also
enterWrite, ScopedWriteLock

◆ tryEnterRead()

bool juce::ReadWriteLock::tryEnterRead ( ) const
noexcept

Tries to lock this object for reading.

Multiple threads can simultaneously lock the object for reading, but if another thread has it locked for writing, then this will fail and return false.

Returns
true if the lock is successfully gained.
See also
exitRead, ScopedTryReadLock

◆ tryEnterWrite()

bool juce::ReadWriteLock::tryEnterWrite ( ) const
noexcept

Tries to lock this object for writing.

This is like enterWrite(), but doesn't block - it returns true if it manages to obtain the lock.

Returns
true if the lock is successfully gained.
See also
enterWrite, ScopedTryWriteLock

◆ tryEnterWriteInternal()

bool juce::ReadWriteLock::tryEnterWriteInternal ( Thread::ThreadID threadId) const
privatenoexcept

Member Data Documentation

◆ accessLock

SpinLock juce::ReadWriteLock::accessLock
private

◆ numWaitingWriters

int juce::ReadWriteLock::numWaitingWriters = 0
mutableprivate

◆ numWriters

int juce::ReadWriteLock::numWriters = 0
private

◆ readerThreads

Array<ThreadRecursionCount> juce::ReadWriteLock::readerThreads
mutableprivate

◆ readWaitEvent

WaitableEvent juce::ReadWriteLock::readWaitEvent
private

◆ writerThreadId

Thread::ThreadID juce::ReadWriteLock::writerThreadId = {}
mutableprivate

◆ writeWaitEvent

WaitableEvent juce::ReadWriteLock::writeWaitEvent
private

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