JUCE
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
Classes | Typedefs
juce_CriticalSection.h File Reference

Classes

class  CriticalSection
 A mutex class. More...
class  DummyCriticalSection
 A class that can be used in place of a real CriticalSection object, but which doesn't perform any locking. More...
struct  DummyCriticalSection::ScopedLockType
 A dummy scoped-lock type to use with a dummy critical section. More...

Typedefs

typedef
CriticalSection::ScopedLockType 
ScopedLock
 Automatically locks and unlocks a CriticalSection object.
typedef
CriticalSection::ScopedUnlockType 
ScopedUnlock
 Automatically unlocks and re-locks a CriticalSection object.
typedef
CriticalSection::ScopedTryLockType 
ScopedTryLock
 Automatically tries to lock and unlock a CriticalSection object.

Typedef Documentation

Automatically locks and unlocks a CriticalSection object.

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

e.g.

    CriticalSection myCriticalSection;

    for (;;)
    {
        const ScopedLock myScopedLock (myCriticalSection);
        // myCriticalSection is now locked

        ...do some stuff...

        // myCriticalSection gets unlocked here.
    }
See also:
CriticalSection, ScopedUnlock

Automatically unlocks and re-locks a CriticalSection object.

This is the reverse of a ScopedLock object - instead of locking the critical section for the lifetime of this object, it unlocks it.

Make sure you don't try to unlock critical sections that aren't actually locked!

e.g.

    CriticalSection myCriticalSection;

    for (;;)
    {
        const ScopedLock myScopedLock (myCriticalSection);
        // myCriticalSection is now locked

        ... do some stuff with it locked ..

        while (xyz)
        {
            ... do some stuff with it locked ..

            const ScopedUnlock unlocker (myCriticalSection);

            // myCriticalSection is now unlocked for the remainder of this block,
            // and re-locked at the end.

            ...do some stuff with it unlocked ...
        }

        // myCriticalSection gets unlocked here.
    }
See also:
CriticalSection, ScopedLock

Automatically tries to lock and unlock a CriticalSection object.

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

e.g.

    CriticalSection myCriticalSection;

    for (;;)
    {
        const ScopedTryLock 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, ScopedLock, ScopedUnlock, ScopedReadLock