Is this combination of ConcurrentDictionary and ConcurrentQueue thread-safe?

☆樱花仙子☆ 提交于 2019-12-23 07:32:06

问题


I'm using the ConcurrentDictionary and ConcurrentQueue classes from .NET 4 in the following code.

Is this code thread-safe? If not, how can I make it thread-safe?

public class Page
{
    public string Name {get; set; }
}

public class PageQueue
{
    private ConcurrentDictionary<int, ConcurrentQueue<Page>> pages =
        new ConcurrentDictionary<int, ConcurrentQueue<Page>>();

    public void Add(int id, Page page)
    {
        if (!this.pages.ContainsKey(id))
            this.pages[id] = new ConcurrentQueue<Page>();

        this.pages[id].Enqueue(page);
    }

    public Page GetAndRemove(int id)
    {
        Page lp = null;

        if(this.pages.ContainsKey(id))
            this.pages[id].TryDequeue(out lp);

        return lp;
    }
}

Demo:

 public class Demo
 {
     public void RunAll()
     {
         for (int i = 0; i < 10; i++)
             Task.Factory.StartNew(() => Run());
     }

     public void Run()
     {
         PageQueue pq = new PageQueue();
         pq.Add(1, new Page());

         pq.GetAndRemove(1);
     }
 }

回答1:


As @Femaref correctly pointed out, there are some flaws in your code. I suggest you take advantage of the many methods offered by ConcurrentDictionary<K,V> to make your code thread-safe without the need for lock statements:

public class PageQueue
{
    private ConcurrentDictionary<int, ConcurrentQueue<Page>> pages =
        new ConcurrentDictionary<int, ConcurrentQueue<Page>>();

    public void Enqueue(int id, Page page)
    {
        var queue = this.pages.GetOrAdd(id, _ => new ConcurrentQueue<Page>());

        queue.Enqueue(page);
    }

    public bool TryDequeue(int id, out Page page)
    {
        ConcurrentQueue<Page> queue;

        if (this.pages.TryGetValue(id, out queue))
        {
            return queue.TryDequeue(out page);
        }

        page = null;
        return false;
    }
}



回答2:


You can (and probably will) run in to problems with these statements:

    if (!this.pages.ContainsKey(id))
        this.pages[id] = new ConcurrentQueue<Page>();

and

    if(this.pages.ContainsKey(id))
        this.pages[id].TryDequeue(out lp);

as the ConcurrentDictionary can be changed between the if-statement and the Assignment/Dequeue. Use a lock on a locking object for those parts of the code, like:

public class PageQueue
{
    private ConcurrentDictionary<int, ConcurrentQueue<Page>> pages = new ConcurrentDictionary<int, ConcurrentQueue<Page>>();
    private object locker = new object();

    public void Add(int id , Page page)
    {
        lock(locker)
        {
          if (!this.pages.ContainsKey(id))
              this.pages[id] = new ConcurrentQueue<Page>();
        }

        this.pages[id].Enqueue(page);
    }

    public Page GetAndRemove(int id)
    {
        Page lp = null;

        lock(locker)
        {
          if(this.pages.ContainsKey(id))
            this.pages[id].TryDequeue(out lp);
        }

        return lp;
    }
}


来源:https://stackoverflow.com/questions/3548079/is-this-combination-of-concurrentdictionary-and-concurrentqueue-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!