5

The reason behind my question is that I am writing an audio units hosting app for iPhone and I need to synchronize memory access from the audio threads (writing to memory) and from the GUI thread (reading from memory).

While looking for guides to read up on the subject, I figured out that apple provides two guides for parallel programming in iOS (analogous guides are available for OS X):

With regards to iOS, I am a novice to parallel programming; thus it is not clear to me which of the guides I need to read, or if they cover the same matters.

Daniel S.
  • 6,458
  • 4
  • 35
  • 78

2 Answers2

5

This is written in a warning box inside of the Threading Programming Guide

Important: If you are developing a new application, you are encouraged to investigate the alternative OS X technologies for implementing concurrency. This is especially true if you are not already familiar with the design techniques needed to implement a threaded application. These alternative technologies simplify the amount of work you have to do to implement concurrent paths of execution and offer much better performance than traditional threads. For information about these technologies, see Concurrency Programming Guide.

So, really the difference is that the Threading Programming Guide is older and discouraged from being used in modern Objective-C.

HOWEVER, as noted in the comments, for operations that need extremely low-latency (like real-time audio editing) are not suited for NSOperation and GCD.

borrrden
  • 33,256
  • 8
  • 74
  • 109
  • This seems to be a good decision in most cases. However, I do not see how to use dispatch queues and operation queues, which are the main building blocks of the concurrency programming guide, to manage access to the shared memory. The problem here seems to be that using the AudioUnit Framework already binds you to have different threads running in your application. I can not avoid this. Can you give an example, following the concurrency programming guide, of how to synchronize the memory access? – Daniel S. Jul 16 '13 at 10:10
  • Well, I won't write an example but I will tell you that that is the exact point of *serial dispatch queues*. If you put all of your operations for shared memory into one of those, you will be thread-safe. Take a look into it, there is a lot of info about it out there. – borrrden Jul 16 '13 at 10:13
  • @borrrden because GCD and NSOperation block, they probably cannot be used in this context – justin Jul 16 '13 at 10:14
  • 1
    @justin I think you're right, but that seems to be the feeling I get from the two guides in general. I will make a note in my answer. – borrrden Jul 16 '13 at 10:15
5

Concurrency Programming Guide will be your crash course in dispatch APIs (aka GCD) and NSOperations.

Threading Programming Guide will introduce you to Threads, Mutual Exclusion and Synchronization APIs and technologies. They will also cover creation of threads and interacting with Run Loops.

For your stated problem, the information in the Threading Programming Guide will be more useful.

However, much of what these guides present is of the mindset that blocking is OK. In realtime Audio, it's not OK.

AudioUnit Hosting Fundamentals is also a required read: http://developer.apple.com/library/ios/#documentation/MusicAudio/Conceptual/AudioUnitHostingGuide_iOS/AudioUnitHostingFundamentals/AudioUnitHostingFundamentals.html

If you are animating your UI, you may need to implement/find a circular buffer implementation.

Beware - concurrency in realtime is going to be a tough subject if you are new to concurrent designs.

justin
  • 104,054
  • 14
  • 179
  • 226