Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Multithreading

Reference counting becomes a bit more complicated in a multithreaded program. The typical setter pattern, shown in Listing 14.1, is problematic in a multithreaded environment. If two threads attempt to invoke setGuitar: at approximately the same time, the second thread can interrupt the first thread before the first thread finishes executing the method. This can result in an object with an incorrect retain count, or a crash if the object that was originally stored in guitar receives two release messages, one from each thread.

Preventing threads from interfering with each other while using the setter method requires the use of a lock, as shown Listing 14.2.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint