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
  • DownloadDownload
  • PrintPrint

pthreads

pthreads work the same way as they do on other platforms. However, if you want to use Cocoa classes in a pthread, you must put Cocoa into multithreaded mode: Cocoa avoids creating locks for single-threaded applications because using mutexes is expensive. (Mutexes provide inter-thread synchronization.) Instead, Cocoa sets all locks to nil at initialization and registers for the NSWillBecomeMultithreadedNotification to create the locks as soon as the application becomes multithreaded. The nil rule means that all lock manipulations are ignored in single-threaded applications.

To put Cocoa in multithreaded mode, your application must create an NSWillBecomeMultithreadedNotification. As NSThread creates this notification when it starts its second thread, all you need to do is create an NSThread that immediately exits before you use pthreads. You can check whether Cocoa is in multithreaded mode at any time by invoking NSThread’s isMultiThreaded. Notifications are discussed in more detail in Hour 12, “Adding Navigation and Tab Bar Controllers.”


  

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