Reputation: 1420
My Mac OSX application has a background process that uses Core Graphics to draw 2D lines and areas into a CGLayer
. This is then periodically displayed to the user via the displayRect()
method of an NSView
subclass.
At the moment I'm using a GCD queue to serialise the tasks that read and write to the layer. This ensures that the background process can never be drawing on it at the same time the main thread draws it into the view using CGContextDrawLayerAtPoint()
. However, this costs me some performance.
Question 1: Is the serialisation of R/W accesses to the CGLayer
necessary for the stability of the app? (I don't mind the odd mis-displayed image, but I don't want to risk a crash).
Question 2: If I were to split the drawing task into several concurrent processes, would it be safe to allow them to all write to the layer at the same time or would I need to serialise them (which would make the concurrency kind of pointless)?
Upvotes: 0
Views: 34
Reputation: 1420
OK so having done some testing I can say the answer to Question (2) is NO it is NOT safe to let two different processes write simultaneously to the same CGLayer
.
When I tried doing two simultaneous CGContextFillRect()
actions on the same layer, the application terminated abnormally with EXC_BAD_ACCESS() code=1
.
However the answer to Question (1) is that is DOES seem to be alright to do a simultaneous read of a CGLayer
while a background process is still writing it. For instance here is the result of reading a layer while a background process is in the process of overwriting all the red squares with blue ones.
Upvotes: 0