Skip to content

Commit

Permalink
add more caveats
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight committed Aug 18, 2023
1 parent 89635b8 commit 940bb3c
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ dispatch_apply(numberOfNumbers, dispatch_get_global_queue(QOS_CLASS_UTILITY, 0),
});
```

Keep in mind that concurrent solutions require analysis of thread safety for the code you're parallelizing. You may need to `@synchronize` critical sections, use semaphores, or dispatch back to a serial queue (or the main queue for UI work). Note that both `enumerateObjects...` and `dispatch_apply` are synchronous and won't return until all iterations have completed. Dispatch their invocation asynchronously off the main queue to avoid waiting.
There are several things to Keep in mind when introducing concurrency:
- you may need to `@synchronize` critical sections, use semaphores, or dispatch back to a serial queue (or the main queue for UI work)
- you may not be able to parallelize loops whose iterations are dependent or where order is significant
- parallelization may not be more efficient for small collections, as [thread spawning has its own costs](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/AboutThreads/AboutThreads.html#//apple_ref/doc/uid/10000057i-CH6-SW20); always measure first!
- both `enumerateObjects...` and `dispatch_apply` are synchronous and won't return until all iterations have completed. Dispatch their invocation asynchronously off the main queue to avoid waiting.

0 comments on commit 940bb3c

Please sign in to comment.