Search code examples
objective-cmultithreadingatomic

Is there a way to run code atomically in Objective-C?


I'm trying to optimise a piece of code that loads a lot of images from the hard drive and does some computations on it. My first attempt to speed things up was to create an NSOperationQueue to load and do the computation in separate NSOperations for each image.

Unfortunately that did not really shorten the time taken.

One reason could be the overhead caused by creating the NSOperations and adding them to the queue. Another possible reason is that the hard drive is not designed to work multithreaded, so instead of reading the files one by one the disk's header needs to jump back and forth between sectors.

So my question would be if there is any way to run obj-C or C code atomically, so that while one NSOperation reads a file it is not disturbed by some operation trying to read another file, but still all NSOperations can do their computations on their image?


Solution

  • Why don't you simply use GCD (Grand Central Dispatch) and forget about explicit threading?

    Here is a good Apple guide about it