• 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: Threading question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Threading question


  • Subject: Re: Threading question
  • From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
  • Date: Sun, 16 May 2004 15:44:39 -0500

Ken Tozier, email@hidden, wrote:
>Which part of a class, methods or data, make it thread-safe?
>I wrote a class that doesn't have any globals and doesn't communicate
>with other objects, just takes inputs, handles them internally and
>spits out results. Would this non-interaction make it automatically
>thread safe?

No. Say you have this method:

- (void) addOne {
_total += 1;
}

This method is not thread-safe. Specifically, if two or more threads
enter this method at once, you can "lose" an increment. One easy way to
make it thread-safe:

- (void) addOne {
@synchronized( self ) {
_total += 1;
}
}

| Jonathan 'Wolf' Rentzsch http://rentzsch.com
| Red Shed Software http://redshed.net
| "better" necessarily means "different"
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Prev by Date: Re: Threading question
  • Next by Date: Re: Recuperating after NS_HANDLER
  • Previous by thread: Re: Threading question
  • Next by thread: Re: Threading question
  • Index(es):
    • Date
    • Thread