gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Trying to understand NSOperation


From: Banlu Kemiyatorn
Subject: Trying to understand NSOperation
Date: Mon, 28 Feb 2011 00:19:45 +0700

Hi, please help me trying to understand the code below which try to
demonstrate op queuing behaviors. Questions are in the comments.

#include <unistd.h>
#import <Foundation/Foundation.h>

@interface MyOp : NSOperation
{
@public
        NSString *name;
        int sec;
}
@end

static MyOp *a;
static MyOp *b;
static MyOp *c;

@implementation MyOp
+ (void) logFin:(NSString *)from
{
        NSLog(@"...in %@, A is%@ finished",from,[a isFinished]?@"":@" not");
        NSLog(@"...in %@, B is%@ finished",from,[b isFinished]?@"":@" not");
        NSLog(@"...in %@, C is%@ finished",from,[c isFinished]?@"":@" not");
}

- (void) main
{
        NSLog(@"%@ starts for %@",[NSThread currentThread],name);
        [MyOp logFin:name];
        sleep(sec);
        [MyOp logFin:name];
}
@end


int main (int argc, const char * argv[]) {              
        [NSAutoreleasePool new];

        a = [MyOp new];
        b = [MyOp new];
        c = [MyOp new];

        /* I am setting up my own operations and I want C to depends on A and
B in the end */
        a->name = @"A"; a->sec = 5;
        b->name = @"B"; b->sec = 10;
        c->name = @"C"; c->sec = 7;

        NSOperationQueue *q = [NSOperationQueue new];
        //[q setMaxConcurrentOperationCount:10];

        /* here I queue A and B first */
        [q addOperation:a];
        [q addOperation:b];
        /* Q: Why B is waiting for A to finish here?
           gwynne of #macdev was testing this for me (thanks!) on 10.6 and it
didn't wait.
           A and B's main are called from different threads
         */

        [c addDependency:a];
        [c addDependency:b];
        [q addOperations:[NSArray arrayWithObject:c]
        /* Q: Why C -main started even B was not yet finished? on the OSX, C
-main started after A and B
           returned from -main */
                waitUntilFinished:YES];
        /* Q: Why should q wait for B to be finished, or should it only wait 
for C ?
           (I don't want to bother him but I forgot to test this in my first
test code I sent) */

        return 0;
}

/* code end  here*/
Could this be something about the changes of NSOperation's behaviors
for isConcurrent et al. from 10.5 to 10.6?

-- 
    .----.     Banlu Kemiyatorn
  /.../\...\   漫画家
 |.../  \...|  http://qstx.blogspot.com (Free Software Advocacy & Development)
 |../    \..|  http://feedbat.blogspot.com (Studio Work For Hire)
  \/      \/   http://groundzerostudiocomplex.blogspot.com (Studio Research)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]