Basic mutex-based lock for thread synchronization in mulle-objc. Note that this may be too heavyweight for very contested locks - consider using mulle_thread_mutex_t directly in such cases.
NSObject
mulle_thread_mutex_t _lock; // The underlying mutex
All methods are marked with
MULLE_OBJC_THREADSAFE_METHOD
:
-init
- Initializes the lock-lock
- Acquires the lock-unlock
- Releases the lock-tryLock
- Attempts to acquire lock without blocking, returns YES if
successful-lockBeforeTimeInterval:
- Attempts to acquire lock before given time interval expiresNSLock *lock = [NSLock new];
// Basic locking
[lock lock];
// ... critical section ...
[lock unlock];
// Using convenience macro
MulleObjCLockDo( lock)
{
// ... critical section ...
} // automatically unlocked
// Try lock without blocking
if ([lock tryLock])
{
// ... got the lock ...
[lock unlock];
}
// Lock with timeout
if ([lock lockBeforeTimeInterval:1.0])
{
// ... got the lock ...
[lock unlock];
}