Derived from: none
Declared in: <support/Autolock.h>
The BAutolock class provides an easy mechanism for automatically locking and unlocking a BLocker object or, more typically, a BLooper. The mechanism is quite simple: All you need to do is statically allocate a BAutolock object (put it on the stack) and pass it a target BLocker or BLooper. For example:
BAutolock autolocker(myWindow);
This serves to lock the BLocker or BLooper (myWindow in the example above). When the BAutolock object is destroyed, it unlocks the target object. Since this happens automatically when the block of code in which the BAutolock object is declared has finished executing, you don't need to remember to balance two lines of code (Lock() and Unlock()). The single line shown above does the trick. However, you may want to make sure the lock is in place before proceeding:
BAutolock autolocker(myWindow); if ( autolocker.IsLocked() ) { . . . }
You also need to be careful about how long the lock is held (when the BAutolock is destroyed). It should not be held for long, so an autolock is appropriate only for blocks of code that are quickly executed.
inline BAutolock(BLooper *looper) inline BAutolock(BLocker *locker) inline BAutolock(BLocker &locker)
Locks the target looper or locker object.
inline ~BAutolock(void)
Unlocks the target BLooper or BLocker.
inline bool IsLocked(void)
Returns true if the target BLooper or BLocker is locked, and false if not.
The Be Book, in lovely HTML, for the BeOS Preview Release.
Copyright © 1997 Be, Inc. All rights reserved.
Be is a registered trademark; BeOS, BeBox, BeWare, GeekPort, the Be logo, and the BeOS logo are trademarks of Be, Inc.
Last modified June 30, 1997.