I have a Main object  which has:

- (void)add2Number: (NSNumber *)n;
{
        self.sum += [ n intValue ];;    //      bound to TextField
        self.count++;                           //      bound to TextField and 
ProgressIndicator
}

The UserInterface has a Start/Stop button which does (on Start):
for (int i = 0; i < 100000; i++ ) [ self add2Number: [ NSNumber numberWithInt: i ]];

Is fast - about 7400 aps (actions or additons per second), but of course cannot be stopped.

So I created an NSThread which does:
for( int i = 0; i < 100000; i++ )    
{
        if ( [ [ NSThread currentThread ] isCancelled ] ) break;
        NSNumber *n = [NSNumber numberWithInt: i];
[ main performSelectorOnMainThread: @selector(add2Number:) withObject: n waitUntilDone: NO];
};

Is even faster (8000 aps) but the Stop-button is unusable. Seems like the performSelector message has a higher priority than the action method of the button.

So I changed to: ... waitUntilDone: YES.
Now the Stop button can be used to cancel the thread, but it is really slow: about 58 aps.
Obviously the switching between worker and main thread is rather costly.

So: what should be done?

What about:
for( int i = 0; i < 100000; i++ )    
{
        if ( [ [ NSThread currentThread ] isCancelled ] ) break;
        NSNumber *n = [NSNumber numberWithInt: i];
        [ lockForSumAndCount lock];
        [  main add2Number:n];
        [ lockForSumAndCount unlock];
};

Fast it is, and stoppable too, but would it be save, given the fact that sum and count are bound to some textFields/progressIndicator?


Kind regards,

Gerriet.

_______________________________________________

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