Loading...
Searching...
No Matches
juce::CriticalSection Class Reference

## Detailed Description

A re-entrant mutex.

A CriticalSection acts as a re-entrant mutex object. The best way to lock and unlock one of these is by using RAII in the form of a local ScopedLock object - have a look through the codebase for many examples of how to do this.

In almost all cases you'll want to declare your CriticalSection as a member variable. Occasionally you may want to declare one as a static variable, but in that case the usual C++ static object order-of-construction warnings should be heeded.

See also
  ScopedLock, ScopedTryLock, ScopedUnlock, SpinLock, ReadWriteLock, Thread, InterProcessLock

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

- juce_core/threads/juce_CriticalSection.h

Inheritance diagram for juce::CriticalSection:

## Public Types

using | ScopedLockType = GenericScopedLock<CriticalSection>
| Provides the type of scoped lock to use with a CriticalSection.
using | ScopedUnlockType = GenericScopedUnlock<CriticalSection>
| Provides the type of scoped unlocker to use with a CriticalSection.
using | ScopedTryLockType = GenericScopedTryLock<CriticalSection>
| Provides the type of scoped try-locker to use with a CriticalSection.

## Public Member Functions

| CriticalSection () noexcept
| Creates a CriticalSection object.
| ~CriticalSection () noexcept
| Destructor.
void | enter () const noexcept
| Acquires the lock.
bool | tryEnter () const noexcept
| Attempts to lock this critical section without blocking.
void | exit () const noexcept
| Releases the lock.

## Member Typedef Documentation

## ScopedLockType

using juce::CriticalSection::ScopedLockType = GenericScopedLock<CriticalSection>

Provides the type of scoped lock to use with a CriticalSection.

## ScopedUnlockType

using juce::CriticalSection::ScopedUnlockType = GenericScopedUnlock<CriticalSection>

Provides the type of scoped unlocker to use with a CriticalSection.

## ScopedTryLockType

using juce::CriticalSection::ScopedTryLockType = GenericScopedTryLock<CriticalSection>

Provides the type of scoped try-locker to use with a CriticalSection.

## Constructors and Destructors

## CriticalSection()

juce::CriticalSection::CriticalSection | ( |  | ) |

noexcept

Creates a CriticalSection object.

Referenced by ~CriticalSection().

## ~CriticalSection()

juce::CriticalSection::~CriticalSection | ( |  | ) |

noexcept

Destructor.

If the critical section is deleted whilst locked, any subsequent behaviour is unpredictable.

References CriticalSection().

## Member Functions

## enter()

void juce::CriticalSection::enter | ( |  | ) | const

noexcept

Acquires the lock.

If the lock is already held by the caller thread, the method returns immediately. If the lock is currently held by another thread, this will wait until it becomes free.

It's strongly recommended that you never call this method directly - instead use the ScopedLock class to manage the locking using an RAII pattern instead.

See also
  exit, tryEnter, ScopedLock

References enter().

Referenced by enter().

## tryEnter()

bool juce::CriticalSection::tryEnter | ( |  | ) | const

noexcept

Attempts to lock this critical section without blocking.

This method behaves identically to CriticalSection::enter, except that the caller thread does not wait if the lock is currently held by another thread but returns false immediately.

Returns
  false if the lock is currently held by another thread, true otherwise.

See also
  enter

References tryEnter().

Referenced by tryEnter().

## exit()

void juce::CriticalSection::exit | ( |  | ) | const

noexcept

Releases the lock.

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

If the enter() 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
  enter, ScopedLock

References exit().

Referenced by exit().

### 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