为什么一开始就有线程竞赛?

时间:2013-03-04 12:32:16

标签: c# locking task

我最近尝试为lock语句编写一个示例。请考虑以下代码:

public partial class Form1 : Form
    {
        private class Concurrency
        {
            private int _myValue;
            private object _locker = new object();
            public int Value
            {
                set
                {
                    lock (_locker)
                    {
                        _myValue = value;
                        Thread.Sleep(new Random().Next(5, 25));
                    }
                }

                get
                {
                    return _myValue;
                }
            }
        }

        private Random _random;
        private Concurrency _concurrency;

        public Form1()
        {
            InitializeComponent();
            _random = new Random();
            _concurrency = new Concurrency();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CreateTask(1);
            CreateTask(2);
        }

        private void CreateTask(int taskId)
        {
            Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < 10; ++i)
                    {
                        int randomNumber = _random.Next(0, 50);

                        Console.WriteLine("Thread {0}, setting value {1}", taskId, randomNumber);
                        _concurrency.Value = randomNumber;
                        Console.WriteLine("Thread {0}, getting value {1}", taskId, _concurrency.Value);

                        Thread.Sleep(_random.Next(5, 15));
                    }
                });
        }
    }

结果是:

Thread 2, setting value 4
Thread 1, setting value 22
Thread 2, getting value 22
Thread 1, getting value 22
Thread 2, setting value 11
Thread 2, getting value 11
Thread 1, setting value 8
Thread 2, setting value 41
Thread 1, getting value 8
Thread 1, setting value 30
Thread 2, getting value 41
Thread 1, getting value 30
Thread 2, setting value 18
Thread 1, setting value 42
Thread 2, getting value 18
Thread 2, setting value 30
Thread 1, getting value 42
Thread 1, setting value 24
Thread 2, getting value 30
Thread 1, getting value 24
Thread 2, setting value 13
Thread 1, setting value 7
Thread 2, getting value 13
Thread 2, setting value 13
Thread 1, getting value 7
Thread 2, getting value 13
Thread 1, setting value 38
Thread 2, setting value 19
Thread 1, getting value 38
Thread 1, setting value 4
Thread 2, getting value 19
Thread 2, setting value 44
Thread 1, getting value 4
Thread 2, getting value 44
Thread 1, setting value 48
Thread 2, setting value 12
Thread 1, getting value 48
Thread 1, setting value 47
Thread 2, getting value 12
Thread 1, getting value 47

正如你所看到的,一切都很好除了第一次设置/获取情况:线程2设置值2但得到22.而且它不是单一情况,它每次都会发生。我知道设置和获取不是原子的,锁定应该围绕任务中的指令设置,但为什么第一次尝试总是失败而其他工作正常呢?

编辑:

我将Concurrency类更新为:

private class Concurrency
        {
            private static Random _random = new Random();
            private int _myValue;
            private object _locker = new object();
            public int Value
            {
                set
                {
                    lock (_locker)
                    {
                        _myValue = value;
                        Thread.Sleep(_random.Next(5, 250));
                    }
                }

                get
                {
                    return _myValue;
                }
            }
        }

请注意,我还在Thread.Sleep中扩展了时间范围。结果是:

Thread 2, setting value 3
Thread 1, setting value 9
Thread 2, getting value 9
Thread 2, setting value 44
Thread 1, getting value 9
Thread 1, setting value 35
Thread 2, getting value 44
Thread 2, setting value 32
Thread 1, getting value 35
Thread 1, setting value 25
Thread 2, getting value 32
Thread 2, setting value 15
Thread 1, getting value 25
Thread 1, setting value 5
Thread 2, getting value 15
Thread 2, setting value 34
Thread 1, getting value 5
Thread 1, setting value 42
Thread 2, getting value 34
Thread 2, setting value 36
Thread 1, getting value 42
Thread 1, setting value 8
Thread 2, getting value 36
Thread 2, setting value 42
Thread 1, getting value 8
Thread 1, setting value 16
Thread 2, getting value 42
Thread 2, setting value 0
Thread 1, getting value 16
Thread 1, setting value 43
Thread 2, getting value 0
Thread 2, setting value 20
Thread 1, getting value 43
Thread 1, setting value 30
Thread 2, getting value 20
Thread 2, setting value 38
Thread 1, getting value 30
Thread 1, setting value 0
Thread 2, getting value 38
Thread 1, getting value 0

没有什么改变。我猜这不是随机的问题,而是其他一些事情。

2 个答案:

答案 0 :(得分:1)

它发生多次,而不仅仅是第一次

您只需查看一次,实际 是您计划中的错误。每当你看到两个“设置......”时,你可能会读到最后一个。想象一下这种情况:

Main Thread 1     Thread 2
Value = 0         
int x1 = Value     
                  Value = 2 
                  int x2 = Value
WriteLine(x1)     
                  WriteLine(x2)

输出正确(线程1为0,线程2为2)。现在假设调度是这样的:

Main Thread 1     Thread 2
Value = 0         
                  Value = 2 
int x1 = Value     
WriteLine(x1)     
                  int x2 = Value
                  WriteLine(x2)

你会得到一个错误的结果,因为对于两个线程你都会读取值2.实际上它不是错误因为锁定的唯一操作是set,那里不能保证线程1的读操作(获取属性值)将在线程2的写操作(属性值的设置)之前执行。

最后看一下this post,你会看到这样的代码如果写的话可能会失败(完全出于同样的原因):

++_concurrency.Value;

答案 1 :(得分:-1)

正如您所指出的,锁定不正确。所以它更像是一个“为什么它似乎工作,除了一开始?”的问题。 (我只是在重述你的问题。)

[编辑]

由于您更改了代码以删除我所讨论的问题,这是另一个想法 - 我认为这就是答案。

你拥有代码的方式,线程退出锁定和读取值之间的时间非常短。

检查你的二传手:

set
{
    lock (_locker)
    {
        _myValue = value;
        Thread.Sleep(_random.Next(5, 25));
    }
}

现在如果thread1在锁内,它将设置_myValue然后休眠。同时,Thread2将等待进入锁定。

当thread1退出睡眠状态时,它立即退出锁定并继续下一行代码,在这种情况下是打印当前值的行:

Console.WriteLine("Thread {0}, getting value {1}", taskId, _concurrency.Value);

除非在退出锁定和解除引用_concurrency.Value之间取消调度thread1,否则它将收到正确的值。由于时间太短,在此期间不太可能被取消安排。

如果thread1 已取消调度,则thread2将能够进入锁定并在thread1取消引用之前更改_myValue

做任何事情来增加线程设置和获取值之间的时间将使得更有可能观察到“不正确”的值。

尝试以下程序,然后取消注释// Try with this sleep uncommented.指示的行。您会看到更多行打印“数字不匹配”。

using System;
using System.Threading;
using System.Threading.Tasks;


namespace Demo
{
    class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Starting");
            CreateTask(1);
            CreateTask(2);
            Console.ReadKey();
        }

        private static void CreateTask(int taskId)
        {
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 10; ++i)
                {
                    int randomNumber = _random.Next(0, 50);

                    Console.WriteLine("Thread {0}, setting value {1}", taskId, randomNumber);
                    _concurrency.Value = randomNumber;
                    // Thread.Sleep(10); // Try with this sleep uncommented.
                    int test = _concurrency.Value;
                    Console.WriteLine("Thread {0}, getting value {1}", taskId, test);

                    if (test != randomNumber)
                    {
                        Console.WriteLine("Number mismatch.");
                    }

                    Thread.Sleep(_random.Next(5, 15));
                }
            });
        }

        private static Random _random = new Random();
        private static Concurrency _concurrency = new Concurrency();

    }

    class Concurrency
    {
        private int _myValue;
        private object _locker = new object();
        public int Value
        {
            set
            {
                lock (_locker)
                {
                    _myValue = value;
                    Thread.Sleep(_random.Next(5, 25));
                }
            }

            get
            {
                return _myValue;
            }
        }

        static Random _random = new Random();
    }
}

那为什么一开始就失败了?好吧,我认为这只是系统启动线程的一种神器。