请问C#有一个" ThreadLocal的"模拟(数据成员)到" ThreadStatic"属性? [英] Does C# have a "ThreadLocal" analog (for data members) to the "ThreadStatic" attribute?

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

问题描述

我发现的ThreadStatic的属性,以最近是非常有用的,但让我现在想的ThreadLocal的的类型属性的让我有在每个线程的基础上的非静态数据成员。

现在我知道,这将有一些不平凡的意义,但是:

有没有这样的事情存在已经内置到C#/网 或因为它似乎到目前为止,答案是否定的,(对于.NET< 4.0)。?,有一种常用的实现了吗?

我能想到的一个合理的方式来实现它自己,但也只是使用的东西已经存在,如果它是可用的。

稻草人的例子来实现我在找什么,如果它不存在的:

 类Foo
{
    [ThreadStatic]
    静态字典<对象,INT> threadLocalValues​​ =新字典<对象,INT>();
    INT设置defaultValue = 0;

    INT ThreadLocalMember
    {
         得到
         {
              int值=设置defaultValue;
              如果(!threadLocalValues​​.TryGetValue(这一点,超时值))
              {
                 threadLocalValues​​ [这] =值;
              }
              返回值;
         }
         集合{threadLocalValues​​ [这] =值; }
    }
}
 

请原谅任何C#无知。我是一个C ++开发者直到最近才进入C#的更有趣的功能和.net

我仅限于.NET 3.0,也许3.5 (项目已/将很快转移到3.5)。

具体使用情况是回调列表是线程特定的(用虚[ThreadLocal的]属性)的一拉:

 类NonSingletonSharedThing
{
     [ThreadLocal的]名单,其中,回拨>回调;

     公共无效ThreadLocalRegisterCallback(回拨somecallback)
     {
         callbacks.Add(somecallback);
     }

     公共无效ThreadLocalDoCallbacks();
     {
         的foreach(在回调VAR回调)
            callback.invoke();
     }
}
 

解决方案

进入.NET 4.0!

如果您是滞留在3.5(或更早),还有的某些功能你应该看看,如 AllocateDataSlot 这应该做你想要的。

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:

Does such a thing exist already built into C#/.net? or since it appears so far that the answer to this is no (for .net < 4.0), is there a commonly used implementation out there?

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; }
    }
}

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

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

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();  
     }
}

解决方案

Enter .NET 4.0!

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#有一个&QUOT; ThreadLocal的&QUOT;模拟(数据成员)到&QUOT; ThreadStatic&QUOT;属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆