Esempio n. 1
0
        private static void SetFieldMayTimes(long n)
        {
            var value = new SomewhatBigStruct(n);;

            while (_isRunning)
            {
                lock (_syncRoot)
                {
                    _field = value;
                }
            }
        }
Esempio n. 2
0
        public static void Execute()
        {
            Task.Run(() => SetFieldMayTimes(1));
            Task.Run(() => SetFieldMayTimes(2));
            Task.Run(() => SetFieldMayTimes(3));

            SomewhatBigStruct copy = new SomewhatBigStruct();

            while (true)
            {
                if (copy.X != copy.Y || copy.X != copy.Z)
                {
                    Console.WriteLine($"X is {copy.X}, Y is {copy.Y}, Z is {copy.Z}");
                }
                lock (_syncRoot)
                {
                    copy = _field;
                }
            }

            _isRunning = false;

            //Console.WriteLine($"X is {copy.X}, Y is {copy.Y}, Z is {copy.Z}. This should not happen!");
        }