Ejemplo n.º 1
0
        public void Scenario4ImList()
        {
            var l = new ImList <D>();

            for (int idx = 1; idx <= cntAdd; idx++)
            {
                l = l.Add(new D(idx));
            }
            for (int idx = 1; idx <= cntRead; idx++)
            {
                var a = l.ToArray();
                if (a.Length != cntAdd)
                {
                    throw new Exception($"{a.Length} != {cntAdd}");
                }
            }
        }
Ejemplo n.º 2
0
        public void Scenario4ImListLock()
        {
            var lst   = new ImList <D>();
            var datas = GetDatas();

            Run(
                () => {
                foreach (var d in datas.datas1)
                {
                    lock (this) {
                        var ll = System.Threading.Volatile.Read(ref lst);
                        ll     = ll.Add(d);
                        System.Threading.Volatile.Write(ref lst, ll);
                        System.Threading.Interlocked.MemoryBarrier();
                    }
                    eatTime();
                }
            },
                () => {
                foreach (var d in datas.datas2)
                {
                    lock (this) {
                        var ll = System.Threading.Volatile.Read(ref lst);
                        ll     = ll.Add(d);
                        System.Threading.Volatile.Write(ref lst, ll);
                        System.Threading.Interlocked.MemoryBarrier();
                    }
                    eatTime();
                }
            }
                );

            for (int idx = 1; idx <= cntRead; idx++)
            {
                var a = lst.ToArray();
                if (a.Length != 2 * cntAdd)
                {
                    throw new Exception($"{a.Length} != 2*{cntAdd}");
                }
            }
        }