C# 6 Multi-point collections instead of masses



  • There is a mass int[][a] that requires multiple access to all data transactions. To this end, the ConcurrentDictionary multi-point collections should be used. I'll make it hard to choose between ConcurrentDictionary<int,int[]> and ConcurrentDictionary<int,ConcurrentDictionary<int,int[]>>(close the number of the mass element). What's the difference? Does it make sense to record another Concurrent-Clution as shown in the second case and why? Will conflicts occur in any of these options?



  • Substitution of mass ConcurrentDictionary For security reasons, it's quite a strange idea, because most operations ConcurrentDictionary It doesn't make sense.

    In fact, the only operations over the mass are the element receipt and updating. But in order to get a little bit safe, there's no need to accumulate, because reading. int always atomarno. To update the body of the mass, any Interlocked- surgery. Okay, method. TryUpdate of ConcurrentDictionary directly corresponding to the so-called operation https://msdn.microsoft.com/ru-ru/library/801kt583(v=vs.110).aspx :

    Interlocked.CompareExchange(ref a[5], 10, 20);
    

    But perhaps you didn't need a mass of reference: you needed to add elements or remove them. In that case, you did the right thing to do to replace the mass with something else, because the re-establishment of the atomary force is not an operation. What kind of collection do you have to use, depends solely on what you have to do with it.

    I can only advise you to try to use different options and see if each of them can only use treasury operations.

    I also propose another possible substitute for such a set:

    • ConcurrentDictionary<int,int[]>
    • ConcurrentDictionary<int,ConcurrentDictionary<int, int>>
    • ConcurrentDictionary<Tuple<int, int>, int>

    But I have to warn you, the combination of a few sensitive operations is far from always safe in general. If you feel like you're messing with this, maybe you shouldn't try to use lock-free algorithms, but rather a "traditional" approach with locks.

    Simple use of design lock Make any operation safe (if, of course, you'll use the challenge. lock with the same object for any reference to the mass.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2