NSCondition

Thread synchronization primitive that combines mutex and condition variable functionality. Provides low-level thread coordination.

Base Class

NSObject

Implemented Protocols

Instance Variables

#ifndef _WIN32
pthread_mutex_t   _lock;       // Mutex for synchronization
pthread_cond_t    _condition;  // Condition variable
#endif

Methods

NSLocking Protocol

Condition Operations

Properties

Usage Example

// Create condition
NSCondition *condition = [[NSCondition alloc] init];

// Producer thread
[condition lock];
// ... modify shared state ...
[condition signal];  // or [condition broadcast];
[condition unlock];

// Consumer thread
[condition lock];
while (!readyToProcess) {
    [condition wait];
}
// ... process shared state ...
[condition unlock];

// Try lock without blocking
if ([condition tryLock]) {
    // ... critical section ...
    [condition unlock];
}

Important Notes

  1. Thread Safety
  2. Implementation Details
  3. Usage Guidelines
  4. Best Practices
  5. Common Pitfalls