Search code examples
.netasync-awaittask-parallel-libraryexecutioncontext

About thread switching, hopping and thread reuse in TPL with respect to AsyncLocal


When using async/await in TPL, will it reuse idle threads and in theory invoke callbacks on other threads than used initially?

About AsyncLocal in this context, will it pass its value around to match the flow or could I end up with a value from another reused thread?


Solution

  • Async-await will pick a random thread from the thread-pool, unless there is a SynchronizationContext, which can force TPL to continue on the same thread.

    The idea of AsyncLocal is to persist a value across threads. It is ThreadLocal that is thread-specific.