Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C / Cocoa equivalent of C# ManualResetEvent

Is there an equivalent of the .NET ManualResetEvent class available for use in Objective-C / Cocoa?

like image 350
Lounges Avatar asked Jul 07 '09 22:07

Lounges


People also ask

What is Objective-C Cocoa?

Objective-C is the language... it defines all the things like the keywords for defining objects, the syntax for messaging object, things like that. Cocoa is a development framework (it's actually an umbrella framework which combines three other frameworks, Foundation, AppKit and CoreData).

What is cocoa in C?

It is an integrated set of shared object libraries, a runtime system, and a development environment. Cocoa provides most of the infrastructure that graphical user applications typically need and insulates those applications from the internal workings of the core operating system.

Is Objective-C superset of C?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.

Is Objective-C just C?

Objective-C is a thin layer atop C and is a "strict superset" of C, meaning that it is possible to compile any C program with an Objective-C compiler and to freely include C language code within an Objective-C class. Objective-C derives its object syntax from Smalltalk.


2 Answers

I'm not very familiar with ManualResetEvent, but based on the documentation, it looks like the NSCondition class might be what you are looking for.

NSCondition is by no means an exact equivalent, but it does provide similar signaling functionality. You might also want to read up on NSLock.

like image 180
Naaff Avatar answered Sep 29 '22 12:09

Naaff


Here is a wrapper class I created which emulates ManualResetEvent using NSCondition.

@interface WaitEvent : NSObject {
    NSCondition *_condition;
    bool _signaled;
}

- (id)initSignaled:(BOOL)signaled;
- (void)waitForSignal;
- (void)signal;

@end

@implementation WaitEvent

- (id)initSignaled:(BOOL)signaled
{
    if (self = ([super init])) {
        _condition = [[NSCondition alloc] init];
        _signaled = signaled;
    }

    return self;
}

- (void)waitForSignal
{
    [_condition lock];
    while (!_signaled) {
        [_condition wait];
    }

    [_condition unlock];
}

- (void)signal
{
    [_condition lock];
    _signaled = YES;
    [_condition signal];
    [_condition unlock];
}

@end

I've done just some basic testing but I think it should get the job done with much less ceremony.

like image 21
Chris Gillum Avatar answered Sep 29 '22 13:09

Chris Gillum