Пример #1
0
        protected override void Resize()
        {
            int    oldCapacity = capacity;
            Thread me          = Thread.CurrentThread;

            if (owner.CompareAndSet(null, me, false, true))
            {
                try
                {
                    if (capacity != oldCapacity)
                    { // someone else resized first
                        return;
                    }

                    foreach (Mutex m in _lock)
                    {
                        m.WaitOne();
                        m.ReleaseMutex();
                    }

                    List <T>[,] oldTable = table;
                    capacity             = 2 * capacity;
                    table = new List <T> [2, capacity];

                    _lock = new Mutex[2, capacity];
                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = 0; j < capacity; j++)
                        {
                            _lock[i, j] = new Mutex();
                        }
                    }

                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = 0; j < capacity; j++)
                        {
                            table[i, j] = new List <T>(PROBE_SIZE);
                        }
                    }

                    foreach (List <T> set in oldTable)
                    {
                        foreach (T z in set)
                        {
                            Add(z);
                        }
                    }
                }
                finally
                {
                    owner.Set(null, false);
                }
            }
        }
        protected override void Resize()
        {
            int    oldCapacity = capacity;
            Thread me          = Thread.CurrentThread;

            if (owner.CompareAndSet(null, me, false, true))
            {
                try
                {
                    if (capacity != oldCapacity)
                    { // someone else resized first
                        return;
                    }
                    for (int i = 0; i < locks.GetLength(1); i++)
                    {
                        locks[0, i].WaitOne();
                    }
                    capacity             = 2 * capacity;
                    List <T>[,] oldTable = table;
                    table = new List <T> [2, capacity];
                    locks = new Mutex[2, capacity];
                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = 0; j < capacity; j++)
                        {
                            locks[i, j] = new Mutex();
                        }
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = 0; j < capacity; j++)
                        {
                            table[i, j] = new List <T>(LIST_SIZE);
                        }
                    }

                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = 0; j < capacity; j++)
                        {
                            foreach (T z in oldTable[i, j])
                            {
                                Add(z);
                            }
                        }
                    }
                }
                finally
                {
                    owner.Set(null, false);
                }
            }
        }