• CallContext类


    CallContext类

    System.Runtime.Remoting.Messaging.CallContext类

    MSDN:

    CallContext is a specialized collection object similar to a Thread Local Storage for method calls and provides data slots that are unique to each logical thread of execution. The slots are not shared across call contexts on other logical threads. Objects can be added to the CallContext as it travels down and back up the execution code path, and examined by various objects along the path.

    本来是很清楚的一个说明。不知道从什么地方看到,从当前线程启动的新线程或异步线程会复制当前线程的CallContext Data。Damn! Is it so?

    ContractedBlock.gifExpandedBlockStart.gifCode
        class Program
        {
            static void Main(string[] args)
            {
                C1<int, string> x = new C1<int, string>();
                x.Prop1 = 999;
                x.Prop2 = "bbac";
                string s = x.Prop2 + x.Prop1.ToString();
                GetSetCallContextData();
            }

            private static void GetSetCallContextData()
            {
                DateTime now = DateTime.Now;
                Console.WriteLine("Set outer thread time:{0}", now);
                CallContext.SetData("now", now);

                Thread.Sleep(3000);

                AutoResetEvent are = new AutoResetEvent(false);
                Thread t = new Thread(new ParameterizedThreadStart((x) =>
                {
                    AutoResetEvent outerAre = x as AutoResetEvent;

                    object outerTime = CallContext.GetData("now");
                    Console.WriteLine("Get outer thread time for inner thread:{0}", outerTime);

                    DateTime innerNow = DateTime.Now;
                    Console.WriteLine("Set inner thread time:{0}", innerNow);
                    CallContext.SetData("now", innerNow);

                    outerAre.Set();
                }));
                t.Start(are);


                are.WaitOne();

                DateTime theTime = (DateTime)CallContext.GetData("now");
                Console.WriteLine("Get time from outer thread:{0}", theTime);


                Func<bool> m = () =>
                {
                    object theTime2 = CallContext.GetData("now");
                    Console.WriteLine("Get time from outer thread for async thread:{0}", theTime2);
                    return true;
                };

                var result = m.BeginInvoke((x) => { }, 1);
                m.EndInvoke(result);

                Console.Read();
            }

    经过测试发现,没错,我被人忽悠了!

    可以看到,子线程和异步线程都无法访问到主线程在CallContext中保存的数据。

    另外一点,当使用ASP.NET的时候,虽然线城池里的线程是复用的,但是CallContext并不在一个线程的多次使用中共享。因为CallContext是针对逻辑线程的TLS,线程池中被复用的线程是操作系统中的内核对象而不是托管对象。就像数据库连接池中保存的是非托管资源而不是托管资源。因此,先后执行的两个托管线程可能在底层复用了一个物理线程(内核对象),但并不能共享同一组CallContext数据槽。就像先后new的两个SqlConnection对象可能在底层使用了同一个物理连接,但是托管对象的属性已经被重置。

    与此对照的是ThreadStaticAttribute,标记上这个特性的静态字段是往物理线程的TLS中保存数据(根据MSDN的描述猜的。具体没试过),因此如果两个托管线程对象内部使用的是同一个物理线程,则这个字段会复用(在两个线程通过这一字段访问同一个数据槽)。

    哎,算了。虽让哥们闲得没事干呢?把ThreadStatic的测试代码也贴出来:

    ContractedBlock.gifExpandedBlockStart.gifCode
       class Program
        {
            [ThreadStatic]
            static int a;

            static void Main(string[] args)
            {
                ThreadPool.SetMaxThreads(1, 1);

                ThreadPool.QueueUserWorkItem((x) =>
                {
                    a = 1997;

                });

                ThreadPool.QueueUserWorkItem((x) =>
                {
                    Console.WriteLine("a={0}", a);

                });

                Thread.Sleep(2000);
                Console.WriteLine(a);

                Console.Read();

            }
    }

    可以看到我的猜测是正确的。ThreadStatic的效果是由JIT实现的。

    园子里有个牛人博客的标题是First we try, then we trust.

    到我这是:Fist I guess, then I wrong, then I try, then I trust. 

    转载于:https://www.cnblogs.com/zhy2002/archive/2008/11/18/1335821.html

  • 相关阅读:
    LVM : 简介
    Linux 磁盘与磁盘分区
    Terraform:创建 Azure 虚机
    Terraform:简介
    Jenkins CLI 命令详解
    Bash 中常见的字符串操作
    读书笔记2014第16本:《视觉繁美:信息可视化方法与案例解析》
    读书笔记2014第15本:《视不可当----信息图与可视化传播》
    读书笔记2014第14本:《李鸿章传》
    中国象棋引擎的C#源代码
  • 原文地址:https://www.cnblogs.com/wfy680/p/12315741.html
Copyright © 2020-2023  润新知