« XCode Tip #1: Quick Documentation LookupHappy New Year! »

Mutexes in Cocoa

01/03/07

Permalink 09:57:57 pm, by rekle Email , 77 words   English (US)
Categories: Cocoa

Mutexes 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];
}

No feedback yet

February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        
Copyright © 2005 - 2011, Rick Ekle

Comments? Contact me at rick@ekle.us or visit me on Twitter at @rekle

Search

XML Feeds

User tools

powered by b2evolution free blog software