• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSOperationQueue question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSOperationQueue question


  • Subject: Re: NSOperationQueue question
  • From: Chris Hanson <email@hidden>
  • Date: Fri, 07 Aug 2009 17:49:05 -0700

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@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSOperationQueue question
      • From: Tim Murison <email@hidden>
References: 
 >Re: NSOperationQueue question (From: Tim Murison <email@hidden>)

  • Prev by Date: Re: [myNSWindow setDocumentEdited:dirtyB] fail ??
  • Next by Date: Re: adding a sub menu to multiple super menus?
  • Previous by thread: Re: NSOperationQueue question
  • Next by thread: Re: NSOperationQueue question
  • Index(es):
    • Date
    • Thread