Mutexes in Cocoa
Posted by rekle on Jan 03 2007 in Cocoa
Here's how to do a mutex when writing a multithreaded Cocoa app. Use the NSLock class...
// In your class' header. (ClassName.h) @interface ClassName: NSObject { NSLock* mutex; } // In your class' source. (ClassName.m) @implementation ClassName - (id)init { if ( (self = [super init] ) ) { mutex = [[NSLock alloc] init]; // Other initialization stuff here.. } return self; } - (void)dealloc { if ( mutex ) { [mutex release]; mutex = NULL; } // Other cleanup code here... [super dealloc]; } - (void)someFunction { [mutex lock]; // Do some stuff. [mutex unlock]; }