Home > Software design >  Is there an equivalent of Thread.threadDictionary in Swift structured concurrency?
Is there an equivalent of Thread.threadDictionary in Swift structured concurrency?

Time:11-12

I have a framework that uses Thread.threadDictionary to provide shared state that affects how methods behave when executed inside the same thread.

I want to add support for the new Swift structured concurrency, but AFAIK there's no guarantee that code executed through Swift structured concurrency executes in the same Thread even if it's all scoped in the same Task.

So, how can I share state across async methods without explicitly passing in a state object when using Swift structured concurrency? Is there an equivalent to Thread.threadDictionary that I don't know about?

CodePudding user response:

You're looking for task-local storage, which is accessed via TaskLocal

  • Related