NSCondition & # 8594; Goal c

I am new to Objective-C. I am currently working on threads.

I need to execute threads synchronously. I use NSInvocationOperaionto create a stream.

I have two threads. I need to wait for the 1st thread to signal an event or timeout. Alarm events can be performed using NSConditionLock. How to signal a timeout. I could not use the method waitUntilDatehere since the timeout is not a fixed value. Is there any way to do this?

edited

main.m
------
#import "PseudoSerialQueue.h"
#import "PseudoTask.h"

int main()
{
    PseudoSerialQueue* q = [[[PseudoSerialQueue alloc] init] autorelease];
    [q addTask:self selector:@selector(test0)];
    [q addTask:self selector:@selector(test1)];
    [q addTask:self selector:@selector(test2)];
    [q quit];
    return 0;
}

PseudoTask.h
-----------------

#import <Foundation/Foundation.h>


@interface PseudoTask : NSObject {

    id target_;
    SEL selector_;
    id queue_;

}

@property(nonatomic,readonly)id target;

-(id)initWithTarget:(id)target selector:(SEL)selector queue:(id)queue;
-(void)exec;

@end

PseudoTask.m
-----------------

#import "PseudoTask.h"


@implementation PseudoTask

@synthesize target = target_;

-(id)initWithTarget:(id)target selector:(SEL)selector queue:(id)queue
{
    self = [super init];
    if (self) {
        target_ = [target retain];
        selector_ = selector;
        queue_ = [queue retain];
    }
    return self;
}

-(void)exec
{
    [target_ performSelector:selector_];
}

-(void)dealloc
{
    [super dealloc];
    [target_ release];
    [queue_ release];
}

@end


PseudoSerialQueue.h
----------------------------

#import <Foundation/Foundation.h>
#import "PseudoTask.h"

@interface PseudoSerialQueue : NSObject {

    NSCondition* condition_;
    NSMutableArray* array_;
    NSThread* thread_;

}

-(void)addTask:(id)target selector:(SEL)selector;

@end

PseudoSerialQueue.m
----------------------------

#import "PseudoSerialQueue.h"

@implementation PseudoSerialQueue

-(id)init
{
    self = [super init];
    if (self) {
        array_ = [[NSMutableArray alloc]init];
        condition_ = [[NSCondition alloc]init];
        thread_ = [[NSThread alloc] initWithTarget:self selector:@selector(execQueue) object:nil];
        [thread_ start];
    }
    return self;
}

-(void)addTask:(id)target selector:(SEL)selector
{
    [condition_ lock];
    PseudoTask* task = [[PseudoTask alloc] initWithTarget:target selector:selector queue:self];
    [array_ addObject:task];
    [condition_ signal];
    [condition_ unlock];
}

-(void)quit
{
    [self addTask:nil selector:nil];
}

-(void)execQueue
{
    for(;;)
    {
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init];

        [condition_ lock];

        if (array_.count == 0) {
            [condition_ wait];
        }

        PseudoTask* task = [array_ objectAtIndex:0];
        [array_ removeObjectAtIndex:0];

        [condition_ unlock];

        if (!task.target) {
            [pool drain];
            break;
        }

        [task exec];
        [task release];

        [pool drain];
    }
}

-(void)dealloc
{
    [array_ release];
    [condition_ release];
    [super dealloc];
}

@end

I could not transfer myself from main.Hope, I mistakenly call it. Error: "self" uneclared will come.

I could not understand - (invalid) Exec {[target_ performSelector: selector_]; } in PseudoTask.m

target_ - , ivar. . .

, . , , , .

execQueue , PseudoSerialQueue addTask. addTask quit, nil.I , nil.

, . .

+3
2

NSCondition? waitUntilDate: .

[condition lock];
// wait 5 seconds. 
[condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
[condition unlock];

:

My PseudoSerialQueue , NSObject, .

@interface Test : NSObject
@end

@implementation Test
- (void)test0
{
}

- (void)test1
{
}

- (id)init
{
    self = [super init];
    return self;
}

- (void)exec
{
    PseudoSerialQueue *q = [[PseudoSerialQueue alloc] init];
    [q addTask:self selector:@selector(test0)];
    [q addTask:self selector:@selector(test1)];
    [q addTask:self selector:@selector(test0)];
    [q quit];
}
@end

.

Test *test = [[Test alloc] init];
[test exec];

, nil.

PseudoSerialQueue.

+2

1- ; , 1- (, isDataAvailable).

0

All Articles