Search

ConcurrentDictionary<TKey, TValue>.AddOrUpdate(TKey, Func<TKey, TValue>, Func<TKey, TValue, TValue>) by Tomáš Jecha

Closed
as Fixed Help for as Fixed

2
0
Sign in
to vote
Type: Bug
ID: 693563
Opened: 10/7/2011 5:58:08 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
0
User(s) can reproduce this bug
If using ConcurrentDictionary, its AddOrUpdate method will call "addValueFactory" method more than once if this method is executed in more threads.

There is this line in documentation:
Adds a key/value pair ... if the key does not already exist, or updates a key/value pair ... if the key already exists.
http://msdn.microsoft.com/en-us/library/ee378675.aspx

There should be better documentation of how this works and there must be info, that addValueFactory can be called more than once and its value is not used and added to dictionary in some cases.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

.NET Framework 4

Steps to reproduce

Run this code in console application:

        static ConcurrentDictionary<int, MyObj> dict = new ConcurrentDictionary<int, MyObj>();

        static void Main(string[] args)
        {
            new Thread(() => AddToDict()).Start();
            new Thread(() => AddToDict()).Start();

            Console.ReadLine();
        }

        static void AddToDict()
        {
            dict.AddOrUpdate(1, (i) =>
            {
                // create new
                return new MyObj();
            },
            (i, o) =>
            {
                // update
                o.Update();
                return o;
            });
        }

        class MyObj
        {
            static int counter;
            
            object syncLock = new object();
            string history;

            public MyObj()
            {
                history = string.Format("MyObj #{0} > Created by {1}", counter++, Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine(history);
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            public void Update()
            {
                history += string.Format(" > Updated by {0}", Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine(history);
                }
            }
        }

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

1st thread - "addValueFactory" is called
2nd thread - "addValueFactory" is called and also then "updateValueFactory"
3rt thread - "addValueFactory" is called and also then "updateValueFactory"

Expected results

1st thread - "addValueFactory" is called
2nd thread - "updateValueFactory" is called
3rt thread - "updateValueFactory" is called
File Attachments
0 attachments
Sign in to post a comment.
Posted by ChrisLaMont on 11/18/2012 at 2:23 PM
This is closed, and no update was posted as promised.

The documents haven't been updated. I've needed this information for quite some time now...
Posted by Microsoft on 10/21/2011 at 3:21 PM
Hi, and thank you for your suggestion. I agree that we can better describe how addValueFactory behaves when called from multiple threads. I will put this on my list of content enhancements to consider for a future release of the documentation. I will update you again when the issue is resolved. Thanks again!
Posted by MS-Moderator07 [Feedback Moderator] on 10/9/2011 at 9:39 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Posted by MS-Moderator01 on 10/7/2011 at 6:42 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.