JUCE
Classes | Typedefs
juce_CriticalSection.h File Reference

Classes

class  CriticalSection
 A re-entrant mutex. 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. More...
 
typedef CriticalSection::ScopedUnlockType ScopedUnlock
 Automatically unlocks and re-locks a CriticalSection object. More...
 
typedef CriticalSection::ScopedTryLockType ScopedTryLock
 Automatically tries to lock and unlock a CriticalSection object. More...
 

Typedef Documentation

Automatically locks and unlocks a CriticalSection object.

You can use a ScopedLock as a local variable to provide RAII-based locking of a CriticalSection.

e.g.

1 struct MyObject
2 {
3  CriticalSection objectLock;
4 
5  // assuming that this example function will be called by multiple threads
6  void foo()
7  {
8  const ScopedLock myScopedLock (objectLock);
9 
10  // objectLock is now locked..
11 
12  ...do some thread-safe work here...
13 
14  // ..and objectLock gets unlocked here, as myScopedLock goes out of
15  // scope at the end of the block
16  }
17 };
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.

1 struct MyObject
2 {
3  CriticalSection objectLock;
4 
5  void foo()
6  {
7  {
8  const ScopedLock myScopedLock (objectLock);
9 
10  // objectLock is now locked..
11 
12  {
13  ScopedUnlock myUnlocker (objectLock);
14 
15  // ..and now unlocked..
16  }
17 
18  // ..and now locked again..
19  }
20 
21  // ..and finally unlocked.
22  }
23 };
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.

1 struct MyObject
2 {
3  CriticalSection objectLock;
4 
5  void foo()
6  {
7  const ScopedTryLock myScopedTryLock (objectLock);
8 
9  // Unlike using a ScopedLock, this may fail to actually get the lock, so you
10  // must call the isLocked() method before making any assumptions..
11  if (myScopedTryLock.isLocked())
12  {
13  ...safely do some work...
14  }
15  else
16  {
17  // If we get here, then our attempt at locking failed because another thread had already locked it..
18  }
19  }
20 };
See also
CriticalSection::tryEnter, ScopedLock, ScopedUnlock, ScopedReadLock