开发者

Inheriting ThreadStatic values to implement dynamic scoping in C#/.NET in multithreaded context

开发者 https://www.devze.com 2022-12-17 05:25 出处:网络
Is there a way to make newly-spawned threads inherit the values of ThreadStatic state (or something like it) on their parent threads?I would like to use this (or something like it) to implement \"dyna

Is there a way to make newly-spawned threads inherit the values of ThreadStatic state (or something like it) on their parent threads? I would like to use this (or something like it) to implement "dynamically scope开发者_开发百科d" special variables that contain operation/task context information to use for tracking/logging, etc. Is this a reasonable approach, and can it be made to work?


You can't "inherit" values. However, the new ThreadLocal<T> class for .NET 4 allows you to provide a Func<T> in the constructor, which can initialize the thread based on the parent's state. This would provide a reasonable workaround.

0

精彩评论

暂无评论...
验证码 换一张
取 消