• <bdo id='jR0x3'></bdo><ul id='jR0x3'></ul>
    <i id='jR0x3'><tr id='jR0x3'><dt id='jR0x3'><q id='jR0x3'><span id='jR0x3'><b id='jR0x3'><form id='jR0x3'><ins id='jR0x3'></ins><ul id='jR0x3'></ul><sub id='jR0x3'></sub></form><legend id='jR0x3'></legend><bdo id='jR0x3'><pre id='jR0x3'><center id='jR0x3'></center></pre></bdo></b><th id='jR0x3'></th></span></q></dt></tr></i><div id='jR0x3'><tfoot id='jR0x3'></tfoot><dl id='jR0x3'><fieldset id='jR0x3'></fieldset></dl></div>

  1. <small id='jR0x3'></small><noframes id='jR0x3'>

    <tfoot id='jR0x3'></tfoot>
  2. <legend id='jR0x3'><style id='jR0x3'><dir id='jR0x3'><q id='jR0x3'></q></dir></style></legend>

      C# 是否有“ThreadLocal"?模拟(对于数据成员)到“ThreadStatic";属性?

      时间:2023-07-27

        • <i id='Atcjq'><tr id='Atcjq'><dt id='Atcjq'><q id='Atcjq'><span id='Atcjq'><b id='Atcjq'><form id='Atcjq'><ins id='Atcjq'></ins><ul id='Atcjq'></ul><sub id='Atcjq'></sub></form><legend id='Atcjq'></legend><bdo id='Atcjq'><pre id='Atcjq'><center id='Atcjq'></center></pre></bdo></b><th id='Atcjq'></th></span></q></dt></tr></i><div id='Atcjq'><tfoot id='Atcjq'></tfoot><dl id='Atcjq'><fieldset id='Atcjq'></fieldset></dl></div>
            <tfoot id='Atcjq'></tfoot>

            <legend id='Atcjq'><style id='Atcjq'><dir id='Atcjq'><q id='Atcjq'></q></dir></style></legend>
            • <bdo id='Atcjq'></bdo><ul id='Atcjq'></ul>
                <tbody id='Atcjq'></tbody>

              <small id='Atcjq'></small><noframes id='Atcjq'>

                本文介绍了C# 是否有“ThreadLocal"?模拟(对于数据成员)到“ThreadStatic";属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我最近发现 "ThreadStatic" 属性非常有用,但现在让我想要一个 "ThreadLocal" 类型属性,让我拥有基于每个线程的非静态数据成员.

                I've found the "ThreadStatic" attribute to be extremely useful recently, but makes me now want a "ThreadLocal" type attribute that lets me have non-static data members on a per-thread basis.

                现在我知道这会产生一些重要的影响,但是:

                Now I'm aware that this would have some non-trivial implications, but:

                C#/.net 中是否已经内置了这样的东西? 或者因为到目前为止似乎答案是否定的(对于 .net <4.0),是否存在有一个常用的实现吗?

                我可以自己想出一种合理的方法来实现它,但如果可用的话,我只会使用已经存在的东西.

                I can think of a reasonable way to implement it myself, but would just use something that already existed if it were available.

                稻草人示例,如果它尚不存在,它将实现我正在寻找的东西:

                Straw Man example that would implement what I'm looking for if it doesn't already exist:

                class Foo
                {
                    [ThreadStatic] 
                    static Dictionary<Object,int> threadLocalValues = new Dictionary<Object,int>();
                    int defaultValue = 0;
                
                    int ThreadLocalMember
                    {
                         get 
                         { 
                              int value = defaultValue;
                              if( ! threadLocalValues.TryGetValue(this, out value) )
                              {
                                 threadLocalValues[this] = value;
                              }
                              return value; 
                         }
                         set { threadLocalValues[this] = value; }
                    }
                }
                

                请原谅任何 C# 的无知.我是一名 C++ 开发人员,最近才开始接触 C# 和 .net 更有趣的功能

                Please forgive any C# ignorance. I'm a C++ developer that has only recently been getting into the more interesting features of C# and .net

                我仅限于 .net 3.0,也许是 3.5(项目已经/即将迁移到 3.5).

                I'm limited to .net 3.0 and maybe 3.5 (project has/will soon move to 3.5).

                特定用例是线程特定的回调列表(使用虚构的 [ThreadLocal] 属性) a la:

                Specific use-case is callback lists that are thread specific (using imaginary [ThreadLocal] attribute) a la:

                class NonSingletonSharedThing
                {
                     [ThreadLocal] List<Callback> callbacks;
                
                     public void ThreadLocalRegisterCallback( Callback somecallback )
                     {    
                         callbacks.Add(somecallback);    
                     }
                
                     public void ThreadLocalDoCallbacks();
                     {    
                         foreach( var callback in callbacks )  
                            callback.invoke();  
                     }
                }
                

                推荐答案

                进入.NET 4.0!

                如果您卡在 3.5(或更早版本),则有 一些功能 你应该看看,比如 AllocateDataSlot 应该做你想做的事.

                If you're stuck in 3.5 (or earlier), there are some functions you should look at, like AllocateDataSlot which should do what you want.

                这篇关于C# 是否有“ThreadLocal"?模拟(对于数据成员)到“ThreadStatic";属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:属性构造函数中的 Lambda 表达式 下一篇:是否有一个属性我可以在我的类中使用来告诉 DataGridView 在绑定到列表时不要为其创建列&lt;MyCl

                相关文章

                    <bdo id='5zzTH'></bdo><ul id='5zzTH'></ul>
                1. <small id='5zzTH'></small><noframes id='5zzTH'>

                    <tfoot id='5zzTH'></tfoot>
                  1. <legend id='5zzTH'><style id='5zzTH'><dir id='5zzTH'><q id='5zzTH'></q></dir></style></legend>

                  2. <i id='5zzTH'><tr id='5zzTH'><dt id='5zzTH'><q id='5zzTH'><span id='5zzTH'><b id='5zzTH'><form id='5zzTH'><ins id='5zzTH'></ins><ul id='5zzTH'></ul><sub id='5zzTH'></sub></form><legend id='5zzTH'></legend><bdo id='5zzTH'><pre id='5zzTH'><center id='5zzTH'></center></pre></bdo></b><th id='5zzTH'></th></span></q></dt></tr></i><div id='5zzTH'><tfoot id='5zzTH'></tfoot><dl id='5zzTH'><fieldset id='5zzTH'></fieldset></dl></div>