Note that there are some explanatory texts on larger screens.

plurals
  1. PONSCondition ->Objective C
    text
    copied!<p>I am a newbie to Objective-C. I'm currently working on threads.</p> <p>I have to make a synchronous execution of threads. I'm using <code>NSInvocationOperaion</code> to spawn a thread.</p> <p>I have two threads. I need to wait for the 1st thread to signal a event or the timeout. Signalling a event can be done by <code>NSConditionLock</code>. How to signal a timeout. I could not use <code>waitUntilDate</code> method here as the timeout is not a fixed value. Is there any way to do this?</p> <p>EDITED</p> <pre><code>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 &lt;Foundation/Foundation.h&gt; @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 &lt;Foundation/Foundation.h&gt; #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 </code></pre> <p>I could not pass self from main.Hope i'm mistakenly calling it. Error:'self' undeclared is coming.</p> <p>I could not understand -(void)exec { [target_ performSelector:selector_]; } in PseudoTask.m </p> <p>target_ is not a method and its an ivar. I am not getting any error or warning.But i could not understand that code.</p> <p>I am writing what i have understood from your program.Please correct me if i my way of understanding the program is wrong.</p> <p>The Thread execQueue is spawned when the PseudoSerialQueue is initialised and it waits for the signal from the addTask method. The addTask method is called in the quit method and the parameters passed are nil.I could not understand why to pass a nil parameter.</p> <p>It would be helpful if you explain about it.Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload