Modern Concurrency
in Swift
New

Swift Async Sequence extensions (part 1)

I’ve been trying out various ways to extend and build upon the currently available Concurrency APIs introduced in Swift 5.5 I believe the Concurrency APIs are intentionally somewhat minimal so that developers would intentionally fall back on Combine for complex tasks. Maybe I’m right or wrong but I can see how a lot of people will want to extend the Concurrency APIs to suit better their current tasks. In this post I’ll code a couple of extensions that show how to easily build your own concurrent power-ups. …

Posted on

Extending Xcode with power-ups

In this post I describe the technical setup of a small language experiment I developed to aid myself working on custom Xcode instruments. The need for a more flexible coding inside Xcode Last month, I prototyped a Swift IDE and became very interested in enhancing the experience of building with Swift. (Since Apple doesn’t hire remotely, I wonder what other companies are working in that area?) A little later, I came back to updating my custom Xcode Instrument called Timelane to support debugging new code based on the new Swift modern concurrency APIs: …

Posted on

Announcing: “Modern Concurrency in Swift”

I’m incredibly happy to share that today the new raywenderlich.com book “Modern Concurrency in Swift”, that I’ve been working on, is available at swiftconcurrencybook.com! Together with editors Sandra Grauschopf, Felipe Laso-Marsetti, Richard Turton, and Shai Mishali, we’ve been working very hard to get a book out as soon as possible on the newly released in 2021 Swift Concurrency: The book is written in the classic raywenderlich.com style that mixes key pieces of theory with step-by-step instructions, guiding the readers through working on practical, real-life projects. …

Posted on

Actors, the cooperative pool and concurrency

After I started doing some benchmarking how different APIs perform, when used to build a simple counter, I got really interested to learn more about how the new Swift concurrency model behaves at runtime. So in this post I’ll use a couple of actors and make them do concurrent computations and check how the thread list and dispatch-queues look like in the debugger. The Test Setup I’ve prepared a super-duper simple SwiftUI app that does a bunch of floating-point multiplication and division. …

Posted on

Performance: Actor vs queue vs lock

I do a lot of performance and instrumenting work and I’ve found Peter Steinberger’s post here very useful when comparing lock alternatives. As I worked with async/await and actors more and more this summer, I thought it’d be nice to put together a short post offering some basic benchmarking of actors vs. the existing synchronization mechanisms. Disclaimer Benchmarking depends heavily on the system, temporary conditions, and more. As with any amateur benchmarks, take the numbers in this post with a grain of salt. …

Posted on

The difference between Thread.sleep() and Task.sleep()

With the work-in-progress backport of the new Swift concurrency model all the way to iOS13, async/await and friends are getting more and more relevant for all Swift developers. So here’s a quick and simple example that showcases some of the nice features of the new concurrency model without going into much detail. Thread.sleep() vs Task.sleep() Let’s just look at the old and new sleep APIs: Thread.sleep() is the old API that blocks a thread for the given amount of seconds — it doesn’t load the CPU, the core just idles. …

Posted on

Bridge from Combine to AsyncSequence - the code (p. 2)

Yesterday I wrote about AsyncSequence, AsyncStream and a simple plan how to bridge (or proxy) Combine publishers to the new asynchronous sequences APIs - Bridge from Combine to AsyncSequence - the plan (part 1). Today let’s put together the code as planned and see if that’s going to work out so we can easily use publishers as async sequences. CombineAsyncStream setup I’ll create a new type called CombineAsyncStream and make it conform to AsyncSequence. …

Posted on

Bridge from Combine to AsyncSequence - the plan (p. 1)

Previously, I wrote about actors in Actors part 1 and Actors part 2; this time I’m going to cover how to write a Combine operator to proxy a Combine publisher into an async sequence that you can iterate over by using a simple for loop. Note: The current version of Xcode 13 is beta 2 and I’m going to be using the latest swift.org toolchain nightly snapshot in order to make use of AsyncStream. …

Posted on

Swift Actors: A practical example, part 2

In my previous post Swift Actors: A practical example, part 1 I covered how I got a Swift.org toolchain from trunk, enabled support for the currently experimental support for structured concurrency, and created a thread-safe cache in few lines of Swift code. If you missed part 1, definitely do read it first as this post builds upon the code I already covered there. » When I wrapped up writing part 1, I was looking at my new cache actor and I started wondering “What about Combine? …

Posted on

Swift Actors: A practical example, part 1

I’ve been re-reading the Swift structured concurrency roadmap and the Swift actors proposal and noticed a note on the latter saying: “Partially available in recent main snapshots behind the flag -Xfrontend -enable-experimental-concurrency” So, naturally 🤓, I downloaded the latest snapshot from Swift.org and took it for a spin to try out some actor code! Installing Swift toolchain with actors support Huge disclaimer: this is all experimental experience using a trunk code not cut into a release. …

Posted on