• 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
Messages between Threads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Messages between Threads


  • Subject: Messages between Threads
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Sat, 14 Mar 2009 16:20:07 +0700

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 (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: Messages between Threads
      • From: "Paul Sanders" <email@hidden>
  • Prev by Date: Re: Customizing an NSToolbar to wrap it's tools.
  • Next by Date: Re: Performance problem with GC enabled
  • Previous by thread: Re: How to create an NSDecimal?
  • Next by thread: Re: Messages between Threads
  • Index(es):
    • Date
    • Thread