.NET 4.5 - C# - 在for循环中使用ThreadStatic属性的变量 [英] .NET 4.5 - C# - variable with ThreadStatic attribute in for loop

查看:97
本文介绍了.NET 4.5 - C# - 在for循环中使用ThreadStatic属性的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个简单的类中测试ThreadStatic属性。

这是代码:



I'm testing the ThreadStatic attribute in a simple class.
Here's the code:

public class TestClass
    {
        [ThreadStatic ( )] 
        static int reps = 0;

        [ThreadStatic ( )]
        static int count = 20;

        public void Run ( )
        {
            var t1 = new Task ( ( ) =>
                {
                    for ( int i = 0; i < count; i++ )
                    {
                        Console.WriteLine ( "Thread 1:" + reps++ );
                        Thread.Sleep ( 0 );
                    }
                } );

            var t2 = new Task ( ( ) =>
            {
                for ( int i = 0; i < 20; i++ )
                {
                    Console.WriteLine ( "Thread 2:" + reps++ );
                    Thread.Sleep ( 0 );
                }
            } );



            t1.Start ( );
            t2.Start ( );

            t1.ContinueWith ( ( e ) => 
            { 
                Console.WriteLine ( e.Status ); 
            }, TaskContinuationOptions.OnlyOnRanToCompletion );
            
            t2.ContinueWith ( ( e ) => 
            {
                Console.WriteLine ( e.Status );
            }, TaskContinuationOptions.OnlyOnRanToCompletion );
        }
    }







这是输出:




Here's the output:

Thread 2:0
Thread 2:1
Thread 2:2
Thread 2:3
Thread 2:4
Thread 2:5
Thread 2:6
Thread 2:7
Thread 2:8
Thread 2:9
Thread 2:10
Thread 2:11
Thread 2:12
Thread 2:13
Thread 2:14
Thread 2:15
Thread 2:16
Thread 2:17
Thread 2:18
Thread 2:19
RanToCompletion
RanToCompletion





正如您所看到的,只有第二个任务输出了一些内容两个任务之间的唯一区别是,在第一个任务中,我使用一个标有ThreadStatic属性的静态int变量来计算for循环的退出条件。



为什么第一个任务不输出值?



谢谢



As you can see only the 2nd Task outputs something. The only difference between the two Tasks is that in the first one I use a static int variable marked with the ThreadStatic attribute to calculate the exit condition of the for loop.

Why the first Task does not output the values?

Thanks

推荐答案

这是因为 count 是线程静态的,一个任务正在使用此值,另一个任务使用硬编码的立即常量 20(本身不好)。线程静态字段变为非静态,属性 ThreadStaticAttribute 使其表现为每个线程有两个或更多不同的静态字段,重要的是,单独初始化在每个线程中。它的初始值变为0,因此使用它的循环将经历零次迭代。要看到它,如果足以在每个循环之前输出 count



请参阅: https://msdn.microsoft.com/en-us/library/system .threadstaticattribute%28v = vs.110%29.aspx [ ^ ]。



请注意,拥有这样的字段是没有意义的,因为这是不变的,因此它的值可以用在两个线程中。要使两个任务的循环都有效,您需要声明
This is because count is thread-static, one task is using this value, and another one uses hard-coded immediate constant 20 (bad by itself). A thread-static field becomes not static, the attribute ThreadStaticAttribute makes it behave as two or more different static fields per each thread, which are, importantly, are initialized separately in each thread. Its initial value becomes 0, so the loop using it will undergo zero number of iterations. To see it, if would be enough to output count before each loop.

Please see: https://msdn.microsoft.com/en-us/library/system.threadstaticattribute%28v=vs.110%29.aspx[^].

Note that having such field would be pointless, because this is constant, so its value can be used in two thread. To make both tasks' loops work, you would need to declare
const count = 20; // without the attribute

并从其他地方删除硬编码20。



请注意,您的实验不是100%正确,因为任务不完全是线程



-SA

and remove hard-coded 20 from other places.

Note that your experiment is not 100% correct, because tasks are not exactly threads.

—SA


自己查找MSDN。



https://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx [ ^ ] < br $> b $ b

我的余数:RTFM!
Find out by myself looking on MSDN.

https://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx[^]

Remainder for me: RTFM!


这篇关于.NET 4.5 - C# - 在for循环中使用ThreadStatic属性的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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