-4

Currently I was working on parallel threads in C#. So, I have confusion of using ConcurrentBag<T> and List<T>.

Here is my code:

public async Task<ConcurrentDictionary<string, ConcurrentBag<T>>> MethodA(SearchResults<Result> response)
{
     var a = new ConcurrentDictionary<string, ConcurrentBag<DeviceAlertAlarm>>();
    
     var tasks = response.GetResults().Select(async result) =>
     {
            var b = new List <T>();
            // do something
            a["xxx"] = b;
            
     });
     await Task.WhenAll(tasks);
     return a;
}

For var b = new List ();

Is it mandatory of ConcurrentBag<T> in multi-threading or can I use List<T> which is best way of writing the code with respective of performance.

Which one is better Concurrentbag<T> or List<T> in the above part of code?

halfer
  • 19,824
  • 17
  • 99
  • 186
prasanthi
  • 562
  • 1
  • 9
  • 25
  • Better how? What do you want to do with the things in there? – Sami Kuhmonen Oct 25 '22 at 12:22
  • 1
    If you were not sure how to abstract an question, please show us the concrete problem you have met. – shingo Oct 25 '22 at 12:24
  • @SamiKuhmonen Thank you for quick response. i have edited the question could please check it once. // do something here means calling to db – prasanthi Oct 25 '22 at 12:30
  • 3
    The `List` inside the lambda delegate won't be accessed concurrently so doesn't need to be a concurrent type – Jodrell Oct 25 '22 at 12:34
  • 1
    `ConcurrentDictionary>` would suffice for this example. – Jodrell Oct 25 '22 at 12:38
  • Have you tried `new ConcurrentDictionary>();`? – Theodor Zoulias Oct 25 '22 at 13:07
  • Does this answer your question? [When a ConcurrentBag is better than a List?](https://stackoverflow.com/questions/7193698/when-a-concurrentbag-is-better-than-a-list) – Charlieface Oct 25 '22 at 13:50
  • Hi prasanthi. I see you are getting some negative reactions to your posts. I can't comment on the content of your posts, but I notice your writing tends towards being a bit needy. Technical writing is an expectation here, so "please help me" and "thanks in advance" are best not added. Posts may be regarded as being a bit tatty or in need of improvement with this sort of chatroom stuff, and if readers find they do not like the post, the conversational material may encourage them to downvote. Please try to avoid adding it. Thanks! – halfer Nov 13 '22 at 22:18
  • Boilerplate advice: _Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened._ – halfer Nov 13 '22 at 22:19

1 Answers1

1

Because your inner list is never used concurrently you also do not need to use a ConcurrentBag<T> here.

Commented your example a bit. From what I expect your code is doing I would take ICollection<T> or IEnumerable<T> in the ConcurrentDictionary<string, IEnumerable<T>>.

// IEnumerable, List, Collection ... is enough here.
public async Task<ConcurrentDictionary<string, IEnumerable<T>>> MethodA(SearchResults<Result> response)
{
     var a = new ConcurrentDictionary<string, IEnumerable<DeviceAlertAlarm>>();
    
     var tasks = response.GetResults().Select(async (result) =>
     {    
            //
            // This list just exists and is accessed in this 'task/thread/delegate'
            var b = new List<T>();
            //
            // do something ...
            //
            // The surrounding IDictionary as you have it here 
            // is used *concurrently* so this is enough to be thread-safe
            a["xxx"] = b;
            
     });
     await Task.WhenAll(tasks);
     return a;
}
Martin
  • 3,096
  • 1
  • 26
  • 46