Информация об изменениях

Сообщение Re[3]: Task Parallel Library от 31.01.2017 10:24

Изменено 31.01.2017 11:18 Sinix

Re[3]: Task Parallel Library
Здравствуйте, kov_serg, Вы писали:

S>>... продолжать выполнение после прерывания потока _в произвольной точке_ бессмысленно.

_>WTF? откуда такое утверждение?

Начнём с

When a thread calls Abort on itself, the effect is similar to throwing an exception; the ThreadAbortException happens immediately, and the result is predictable. However, if one thread calls Abort on another thread, the abort interrupts whatever code is running. There is also a chance that a static constructor could be aborted.

(c)

Миль пардон, документацию не обновили. По факту ThreadAbortException не прерывает выполнение static-конструктора

Чуть позже пример приведу.
Re[3]: Task Parallel Library
Здравствуйте, kov_serg, Вы писали:

S>>... продолжать выполнение после прерывания потока _в произвольной точке_ бессмысленно.

_>WTF? откуда такое утверждение?

Начнём с

When a thread calls Abort on itself, the effect is similar to throwing an exception; the ThreadAbortException happens immediately, and the result is predictable. However, if one thread calls Abort on another thread, the abort interrupts whatever code is running. There is also a chance that a static constructor could be aborted.

(c)

Миль пардон, документацию не обновили. По факту ThreadAbortException не прерывает выполнение static-конструктора

Чуть позже пример приведу.

UPD А собственно, чо эт я? Буквально полгода назад ловил, дарю:
  запускать без отладчика
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace LargeArrays
{
    class Program
    {
        private class ListProof
        {
            private static readonly List<int> _list = new List<int>();

            public static void RunListProof()
            {
                while (true)
                {
                    if (_list.Count > 0 && _list.Last() != 42)
                    {
                        Console.WriteLine("Bugged!!!");
                        break;
                    }

                    if (_list.Count > 100 * 1000)
                    {
                        _list.Clear();
                    }
                    _list.TrimExcess();

                    var h = new ManualResetEvent(false);
                    var t = new Thread(
                        () =>
                        {
                            int i;
                            for (i = 0; i < 10; i++)
                            {
                                _list.Add(42);
                            }
                            _list.TrimExcess();
                            h.Set();
                            try
                            {
                                for (; i < 10000; i++)
                                {
                                    _list.Add(42);
                                }
                            }
                            catch (ThreadAbortException)
                            {
                                Console.WriteLine("x" + i);
                            }
                        });
                    t.Start();
                    h.WaitOne();
                    Thread.Sleep(1);
                    t.Abort();
                    t.Join();
                }

                var lastItems = _list.AsEnumerable().Reverse().Take(10).Reverse();
                Console.WriteLine("Last items of list: " + string.Join(", ", lastItems));
            }
        }

        public static void Main(string[] args)
        {
            ListProof.RunListProof();

            Console.WriteLine("complete!");
        }
    }
}


x4945
x5454
x3086
x10
x10
x10
x10
x8809
x470
x5316
x10
x6259
x10
x6146
Bugged!!!
Last items of list: 42, 42, 42, 42, 42, 42, 42, 42, 42, 0
complete!
Для продолжения нажмите любую клавишу . . .


кто расскажет, откуда после вызова _list.Add(42) в списке взялся 0 — тому +1 к пониманию всей прелести проблемы.