C#多线程代码审查

问题描述 投票:1回答:2

锁定队列很重要吗?

public abstract class ExpiringCache<TKey,TValue> : IDisposable 
{
    protected readonly object locker = new object();

    protected readonly Dictionary<TKey, TValue> cache =
        new Dictionary<TKey, TValue>();

    private readonly Queue<KeyValuePair<TKey, long>> queue =
        new Queue<KeyValuePair<TKey, long>>();

    private volatile bool dispose;

    private readonly Thread cleaningThread;

    private readonly long lifeSpanTicks;

    public ExpiringCache(TimeSpan lifeSpan)
    {
        // Validate lifeSpan

        if (lifeSpan.Ticks == 0)
        {
            throw new ArgumentOutOfRangeException
                ("lifeSpan", "Must be greater than zero.");
        }

        this.lifeSpanTicks = lifeSpan.Ticks;

        // Begin expiring expired items

        this.cleaningThread = new Thread(() =>
        {
            while (!this.dispose)
            {
                this.RemoveExpired();
                Thread.Sleep(1);
            }
        });

        this.cleaningThread.Start();
    }

    private void RemoveExpired()
    {
        if (this.queue.Count == 0)
        {
            return;
        }

        var pair = this.queue.Peek();

        if (pair.Value >= DateTime.Now.Ticks)
        {
            lock (this.locker)
            {
                this.cache.Remove(pair.Key);
            }

            this.queue.Dequeue();
        }
    }

    public bool Contains(TKey key)
    {
        lock (this.locker)
        {
            return this.cache.ContainsKey(key);
        }
    }

    public void Add(TKey key, TValue value)
    {
        lock (this.locker)
        {
            this.cache.Add(key, value);
        }

        this.queue.Enqueue(new KeyValuePair<TKey, long>
            (key, DateTime.Now.Ticks + this.lifeSpanTicks));
    }

    public void Dispose()
    {
        this.dispose = true;
    }
}
c# multithreading locking
2个回答
5
投票

例如,仅锁定Contains方法是不够的。看到这个:http://blogs.msdn.com/jaredpar/archive/2009/02/16/a-more-usable-thread-safe-collection.aspx

[基本上,您需要重新考虑经典的Queue API,并使用DequeueIfContains()之类的方法,而不是简单的Contains() + Dequeue(),这样,同一锁既适用于Contains检查,也适用于Dequeue操作。


0
投票

您可以使用:ConcurrentDictionary<TKey, TValue>ConcurrentDictionary<TKey, TValue>。它们内置于C#.Net 4.0及更高版本中。

© www.soinside.com 2019 - 2024. All rights reserved.