How to parallelize across CPU cores?

Thanks, that’s super helpful!

@available(macOS 15.0, *) // For Mutex. We can drop the version if we use a different locking primiative.

Well I suppose this is as good a reason as any to update my operating system…

// A reader-writer lock would be better here, to let multiple reads be concurrent with each other.
// Only the writes actually need exclusive access.
// E.g. https://github.com/SomeRandomiOSDev/ReadWriteLock/blob/main/Sources/ReadWriteLock/ReadWriteAtomic.swift

The ReadWriteLock in that repo is implemented like this:

public class ReadWriteLock {
  private var lock = pthread_rwlock_t()
  ...
}

I recall a number of discussions on this forum where people explained that this pattern was not actual sound, and to properly use a system lock it was necessary to manually allocate an unsafe pointer for it.

I don’t know for sure if that’s still the case, but I also don’t recall hearing about any changes in that regard. Here’s a thread with a really good explanation and discussion of the issues involved: Exposing the Memory Locations of Class Instance Variables

2 Likes