Concurrent Programming in Mac OS X and iOS: Unleash Multicore Performance with Grand Central Dispatch

By Vandad Nahavandipoor

Now that multicore processors are coming to cellular units, would it be nice to exploit all these cores with no need to regulate threads? This concise ebook indicates you ways to exploit Apple's Grand relevant Dispatch (GCD) to simplify programming on multicore iOS units and Mac OS X.

Managing your application’s assets on a couple of middle isn't really effortless, yet it's critical. Apps that use just one center in a multicore surroundings will gradual to a move slowly. in the event you understand how to software with Cocoa or Cocoa contact, this advisor gets you began with GCD instantly, with many examples that will help you write high-performing multithreaded apps.

  • Package your code as block gadgets and invoke them with GCD
  • Understand dispatch queues—the swimming pools of threads controlled through GCD
  • Use diversified tools for executing UI and non-UI tasks
  • Create a gaggle of initiatives that GCD can run all at once
  • Instruct GCD to execute initiatives just once or after a delay
  • Discover how you can build your personal dispatch queues

Show description

Preview of Concurrent Programming in Mac OS X and iOS: Unleash Multicore Performance with Grand Central Dispatch PDF

Best Programming books

Joe Celko's SQL for Smarties: Advanced SQL Programming Third Edition (The Morgan Kaufmann Series in Data Management Systems)

SQL for Smarties used to be hailed because the first booklet dedicated explicitly to the complex ideas had to rework an skilled SQL programmer into a professional. Now, 10 years later and within the 3rd variation, this vintage nonetheless reigns very best because the ebook written by way of an SQL grasp that teaches destiny SQL masters.

Designing Audio Effect Plug-Ins in C++: With Digital Audio Signal Processing Theory

Not only one other theory-heavy electronic sign processing booklet, nor one other uninteresting build-a-generic-database programming e-book, Designing Audio influence Plug-Ins in C++ delivers every little thing you every thing you must recognize to just do that, together with totally labored, downloadable code for dozens audio influence plug-ins and virtually awarded algorithms.

Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition)

“Every C++ specialist wishes a duplicate of powerful C++. it's an absolute must-read for somebody taking into account doing severe C++ improvement. If you’ve by no means learn powerful C++ and also you imagine you recognize every thing approximately C++, reassess. ”— Steve Schirripa, software program Engineer, Google “C++ and the C++ group have grown up within the final fifteen years, and the 3rd version of powerful C++ displays this.

Cocoa Design Patterns

“Next time a few child indicates up at my door soliciting for a code evaluation, this can be the booklet that i'm going to throw at him. ”   –Aaron Hillegass, founding father of colossal Nerd Ranch, Inc. , and writer of Cocoa Programming for Mac OS X   Unlocking the secrets and techniques of Cocoa and Its Object-Oriented Frameworks   Mac and iPhone builders are usually crushed via the breadth and class of the Cocoa frameworks.

Extra info for Concurrent Programming in Mac OS X and iOS: Unleash Multicore Performance with Grand Central Dispatch

Show sample text content

Acting Non-UI-Related initiatives Asynchronously | 31 }); } As you will find in determine 2-2, we now have effectively downloaded our photo and likewise created a picture view to exhibit the picture to the person at the UI. determine 2-2. Downloading and showing photographs to clients, utilizing GCD Let’s flow directly to one other instance. Let’s say that we've got an array of 10,000 random numbers which were kept in a dossier on disk and we wish to load this array into reminiscence, variety the numbers in an ascending style (with the smallest quantity showing first within the list), after which demonstrate the record to the consumer. The keep watch over used for the reveal is dependent upon no matter if you're coding this for iOS (ideally, you’d use an example of UITableView) or Mac OS X (NSTableView will be an exceptional candidate). considering we don’t have an array, why don’t we create the array first, then load it, and eventually exhibit it? listed below are equipment that might support us locate the positioning the place we wish to keep the array of 10,000 random numbers on disk at the machine: - (NSString *) fileLocation{ /* Get the record folder(s) */ NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 32 | bankruptcy 2: Programming Grand valuable Dispatch NSUserDomainMask, YES); /* Did we discover whatever? */ if ([folders count number] == 0){ go back nil; } /* Get the 1st folder */ NSString *documentsFolder = [folders objectAtIndex:0]; /* Append the dossier identify to the top of the records direction */ go back [documentsFolder stringByAppendingPathComponent:@"list. txt"]; } - (BOOL) hasFileAlreadyBeenCreated{ BOOL end result = NO; NSFileManager *fileManager = [[NSFileManager alloc] init]; if ([fileManager fileExistsAtPath:[self fileLocation]] == YES){ end result = sure; } [fileManager release]; go back consequence; } Now the $64000 half: we wish to retailer an array of 10,000 random numbers to disk if and provided that we've not created this array prior to on disk. If we've got, we are going to load the array from disk instantly. If we've not created this array ahead of on disk, we are going to first create it after which stream directly to loading it from disk. on the finish, if the array was once effectively learn from disk, we are going to type the array in an ascending style and at last demonstrate the consequences to the consumer at the UI. i'll go away showing the consequences to the consumer as much as you: dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); /* If we haven't already stored an array of 10,000 random numbers to the disk prior to, generate those numbers from time to time retailer them to the disk in an array */ dispatch_async(concurrentQueue, ^{ NSUInteger numberOfValuesRequired = ten thousand; if ([self hasFileAlreadyBeenCreated] == NO){ dispatch_sync(concurrentQueue, ^{ NSMutableArray *arrayOfRandomNumbers = 33 NSUInteger counter = zero; for (counter = zero; counter < numberOfValuesRequired; counter++){ unsigned int randomNumber = arc4random() % ((unsigned int)RAND_MAX + 1); [arrayOfRandomNumbers addObject: [NSNumber numberWithUnsignedInt:randomNumber; } /* Now let's write the array to disk */ [arrayOfRandomNumbers writeToFile:[self fileLocation] atomically:YES]; [arrayOfRandomNumbers release]; }); } __block NSMutableArray *randomNumbers = nil; /* learn the numbers from disk and type them in an ascending style */ dispatch_sync(concurrentQueue, ^{ /* If the dossier has now been created, we need to learn it */ if ([self hasFileAlreadyBeenCreated] == YES){ randomNumbers = [[NSMutableArray alloc] initWithContentsOfFile:[self fileLocation]]; /* Now type the numbers */ [randomNumbers sortUsingComparator: ^NSComparisonResult(id obj1, identity obj2) { NSNumber *number1 = (NSNumber *)obj1; NSNumber *number2 = (NSNumber *)obj2; go back [number1 compare:number2]; }]; } }); dispatch_async(dispatch_get_main_queue(), ^{ if ([randomNumbers count number] > 0){ /* Refresh the UI right here utilizing the numbers within the randomNumbers array */ } [randomNumbers release]; }); }); 34 | bankruptcy 2: Programming Grand imperative Dispatch There is lots extra to GCD than synchronous and asynchronous block or functionality execution.

Download PDF sample

Rated 4.76 of 5 – based on 23 votes