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
- NSLocking
- MulleObjCThreadSafe
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 expires
NSLock *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];
}
-
Thread Safety
- All methods are thread-safe
- The lock itself provides thread safety for critical sections
-
Performance Considerations
- Consider using mulle_thread_mutex_t directly for high-contention scenarios
- The lockBeforeTimeInterval: method uses thread yielding for polling
-
Platform Specifics
- Some platforms need explicit mutex cleanup in dealloc (controlled by MULLE_THREAD_MUTEX_NEEDS_DONE)