concurrentdictionary

Defending against race conditions in System.Collections.Concurrent.ConcurrentDictionary

ぐ巨炮叔叔 提交于 2019-12-06 04:07:36
问题 The .NET ConcurrentDictionary is susceptible to a race condition that may cause unexpected data as explained at the bottom of this MSDN article. I'm assuming that there are several factors to take into account. Q: How should I write code that is not vulnerable to that race condition that may cause data loss? In my scenario I have an input stream that has an always increasing index (n++). My thought is that I could detect missing data if the race condition occurs and re-send it. On the other

Calling ToList() on ConcurrentDictionary<TKey, TValue> while adding items

橙三吉。 提交于 2019-12-04 02:55:57
问题 I've run into an interesting issue. Knowing that the ConcurrentDictionary<TKey, TValue> is safely enumerable while being modified, with the (in my case) unwanted side-effect of iterating over elements that may disappear or appear multiple times, I decided to create a snapshot myself, using ToList() . Since ConcurrentDictionary<TKey, TValue> also implements ICollection<KeyValuePair<TKey, TValue>> , this causes the List(IEnumerable<T> collection) to be used, which in turn creates an array in

Tuple vs string as a Dictionary key in C#

妖精的绣舞 提交于 2019-12-03 14:41:03
问题 I have a cache that I implement using a ConcurrentDictionary, The data that I need to keep depends on 5 parameters. So the Method to get it from the cache is: (I show only 3 parameters here for simplicity, and I changed the data type to represent CarData for clearity) public CarData GetCarData(string carModel, string engineType, int year); I wonder what type of key will be better to use in my ConcurrentDictionary, I can do it like this: var carCache = new ConcurrentDictionary<string, CarData>

How can I convert a ConcurrentDictionary to a Dictionary?

主宰稳场 提交于 2019-12-03 09:38:11
I have a ConcurrentDictionary object that I would like to set to a Dictionary object. Casting between them is not allowed. So how do I do it? LukeH The ConcurrentDictionary<K,V> class implements the IDictionary<K,V> interface, which should be enough for most requirements. But if you really need a concrete Dictionary<K,V> ... var newDictionary = yourConcurrentDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, yourConcurrentDictionary.Comparer); // or... // substitute your actual key and value types in place of TKey and TValue var newDictionary = new Dictionary<TKey, TValue>

Tuple vs string as a Dictionary key in C#

▼魔方 西西 提交于 2019-12-03 04:25:15
I have a cache that I implement using a ConcurrentDictionary, The data that I need to keep depends on 5 parameters. So the Method to get it from the cache is: (I show only 3 parameters here for simplicity, and I changed the data type to represent CarData for clearity) public CarData GetCarData(string carModel, string engineType, int year); I wonder what type of key will be better to use in my ConcurrentDictionary, I can do it like this: var carCache = new ConcurrentDictionary<string, CarData>(); // check for car key bool exists = carCache.ContainsKey(string.Format("{0}_{1}_{2}", carModel,

Is the list order of a ConcurrentDictionary guaranteed?

江枫思渺然 提交于 2019-12-02 00:48:53
I am using a ConcurrentDictionary to store log-lines, and when I need to display them to the user I call ToList() to generate a list. But the weird thing is that some users receive the most recent lines first in the list, while they should logically be last. Is this because ConcurrentDictionary doesnt guarantee a persistent order on the IEnumerate interface, or what can be the reason? No ConcurrentDictionary (and Dictionary<T> for that matter) does not guarantee the ordering of the keys in the list. You'll have to use a different data type or perform the sorting yourself. For non-concurrent

Caching asynchronous operations

℡╲_俬逩灬. 提交于 2019-11-30 12:40:29
问题 I am looking for an elegant way of caching the results of my asynchronous operations. I first had a synchronous method like this: public String GetStuff(String url) { WebRequest request = WebRequest.Create(url); using (var response = request.GetResponse()) using (var sr = new StreamReader(response.GetResponseStream())) return sr.ReadToEnd(); } Then I made it asynchronous: public async Task<String> GetStuffAsync(String url) { WebRequest request = WebRequest.Create(url); using (var response =

Caching asynchronous operations

喜你入骨 提交于 2019-11-30 03:23:38
I am looking for an elegant way of caching the results of my asynchronous operations. I first had a synchronous method like this: public String GetStuff(String url) { WebRequest request = WebRequest.Create(url); using (var response = request.GetResponse()) using (var sr = new StreamReader(response.GetResponseStream())) return sr.ReadToEnd(); } Then I made it asynchronous: public async Task<String> GetStuffAsync(String url) { WebRequest request = WebRequest.Create(url); using (var response = await request.GetResponseAsync()) using (var sr = new StreamReader(response.GetResponseStream())) return

Why does ConcurrentDictionary.GetOrAdd(key, valueFactory) allow the valueFactory to be invoked twice?

一曲冷凌霜 提交于 2019-11-27 05:28:33
问题 I am using a concurrent dictionary as a thread-safe static cache and noticed the following behavior: From the MSDN docs on GetOrAdd: If you call GetOrAdd simultaneously on different threads, addValueFactory may be called multiple times, but its key/value pair might not be added to the dictionary for every call. I would like to be able to guarantee that the factory is only called once. Is there any way to do this with the ConcurrentDictionary API without resorting to my own separate

.NET - Dictionary locking vs. ConcurrentDictionary

微笑、不失礼 提交于 2019-11-26 17:04:48
I couldn't find enough information on ConcurrentDictionary types, so I thought I'd ask about it here. Currently, I use a Dictionary to hold all users that is accessed constantly by multiple threads (from a thread pool, so no exact amount of threads), and it has synchronized access. I recently found out that there was a set of thread-safe collections in .NET 4.0, and it seems to be very pleasing. I was wondering, what would be the 'more efficient and easier to manage' option, as i have the option between having a normal Dictionary with synchronized access, or have a ConcurrentDictionary which