Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this combination of ConcurrentDictionary and ConcurrentQueue thread-safe?

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);
     }
 }
like image 901
RuSh Avatar asked Aug 23 '10 13:08

RuSh


People also ask

Is ConcurrentDictionary values thread-safe?

ConcurrentDictionary<TKey,TValue>. This collection class is a thread-safe implementation. We recommend that you use it whenever multiple threads might be attempting to access the elements concurrently.

Is ConcurrentDictionary keys thread-safe?

ConcurrentDictionary Represents a thread-safe collection of key-value pairs that can be accessed by multiple threads concurrently.

Is ConcurrentQueue thread-safe?

ConcurrentQueue is a thread-safe FIFO data structure. It's a specialized data structure and can be used in cases when we want to process data in a First In First Out manner.

Do you need to lock a ConcurrentQueue?

While for ConcurrentQueue you do not need to specifically lock as the to Enqueue/Dequeue would have the necessary item level locks. "Simple queue" is usually the problem.


1 Answers

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;
    }
}
like image 171
dtb Avatar answered Nov 15 '22 15:11

dtb