[swift-users] Swift and Threads

Quinn "The Eskimo!" eskimo1 at apple.com
Tue Sep 13 02:49:30 CDT 2016


On 13 Sep 2016, at 05:03, Gerriet M. Denkmann via swift-users <swift-users at swift.org> wrote:

> Or is the code fundamentally wrong …?

This one.

You’re accessing a mutable value (`bitfield`) from multiple threads.  Swift does not support that, even in situations like this where you’re sure that the items are preallocated and that the threads are accessing non-overlapping values.

As to how you fix this, it kinda depends on your final use for the array.  If you’re OK with the results being split into per-thread chunks, you can have each thread work on its own chunk and then pass the results back to the ‘main’ thread when it’s done.  OTOH, if you need your results in one big array, things get tricky.

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware




More information about the swift-users mailing list