Not only does the below code need to use -[NSAutoreleasePool drain] rather than -release, but it also doesn’t start the background collection thread.

The first thing it should do in main() is invoke objc_startCollectorThread() (from <objc/objc-auto.h>) which will ensure GC is happening asynchronously, rather than only when the autorelease pool is drained.

  — Chris

On Aug 7, 2009, at 2:06 PM, Tim Murison wrote:

#import <Cocoa/Cocoa.h>
#include <libkern/OSAtomic.h>

volatile int64_t globalCounter;

@interface Operation : NSOperation
+ (Operation*) operation;
@end

@implementation Operation
+ (Operation*) operation
{
       return [[[Operation alloc] init] autorelease];
}

- (void) main
{
       OSAtomicIncrement64(&globalCounter);
}
@end

int main(int argc, char **argv)
{
       NSOperationQueue *queue = [[NSOperationQueue alloc] init];
       int64_t last, tmp;

       last = tmp = 0;
       [queue setMaxConcurrentOperationCount:4];

       while (1) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
               int64_t tmp;

               [queue addOperation:[Operation operation]];
               tmp = globalCounter;

               if (tmp - last > 1000) {
                       last = tmp;
                       printf("Queue length:%d\n", [[queue operations]
count]);
                       usleep(1000 * 1000);
               }

               [pool release];
       }
}


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to