On Aug 7, 2009, at 12:06, Tim Murison wrote:

The code is below and was compiled with objc-gc-only. If I run the program, despite the setMaxConcurrentOperationCount:4, it will use 65 threads. Also
it will leak memory at ~1Mb/sec.

#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];
}

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

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

       [queue setMaxConcurrentOperationCount:4];

       while (1) {
               int64_t tmp;

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

               if (tmp % 100 == 0) {
                       [[NSGarbageCollector defaultCollector]
collectExhaustively];
                       printf("Counter: %lld\n", tmp);
               }
       }
}

'setMaxConcurrentOperationCount' specifies the number of concurrently executing threads. I don't see anything in the documentation that guarantees it will control the number of concurrently existing threads. Threads may exist waiting to execute, and threads will exist for some period of time after they finish execution (in the NSOperation sense of isExecuting, in all cases).

Also, because globalCounter is being changed by the operations, there's no guarantee the main thread will ever see tmp%100==0, if you're unlucky, so that collectExhaustively thing is unreliable.

Also, because you're putting the main thread in a hard loop, you're stealing CPU time that might be used for operations and/or the garbage collector. Your main thread and your operations are all asking for 100% of the CPU cores they're running in, which is going to make it hard for the garbage collector to get the compute time it needs.

All in all, it makes me want to ask what you're trying to achieve by this. To prove that you can outrun the collector? You can.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to