Thursday, January 5, 2023

Limiting Swift Concurrency’s Cooperative Pool

Alejandro Martinez:

In Swift Concurrency all your async code runs in a cooperative thread pool, unless you are using Actors (or in the future custom Executors, but that’s a topic for another day). The beauty of the cooperative pool abstraction is that it allows us to not care about thread exhaustion, or even about our code running “on the background”, something I still see many developers get wrong.

[…]

In fact this cooperative pool is such a specific implementation detail that it could work totally different in other operative systems or more constrained enviornments.

[…]

And you can test how your code would behave in such a constrained system thanks to an environment variable that limits the cooperative pool.

Set LIBDISPATCH_COOPERATIVE_POOL_STRICT=1 and see how the runtime will only create 1 thread on the pool.

Comments RSS · Twitter · Mastodon

Leave a Comment