-1

Let's say you have a non synchronized list:

List<string> somelist = new List<string>();

Now from multiple threads you add to this list:

List<ProcessItem> processItems = new List<ProcessItem>();
// Create some items in process items
Parallel.ForEach(processItems, (nextProcessItem) => {
    somelist.Add(nextProcessItem.Id)
});

Now we try to use this list in another call:

Parallel.ForEach(somelist, (nextListItem) => {
    // Intermittently some nextListItem are coming in null
    Console.Write(nextListItem);
});

What is the expected result? I'm trying to debug a client's program and I noticed they were doing something just like this scenario. And the result was certain entries in somelist were actually null intermittently.

I changed them to ConcurrentQueue instead hoping it solves the issue.

Is that the expected behavior one would see from adding to a non-synchronized list in multiple threads?

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
  • 3
    Bad ThingsTM Your correct that this is a bug. There's no need to further dissect it. – Damien_The_Unbeliever May 17 '18 at 13:27
  • 3
    Expected behavior only makes sense if you follow the rules. If you don't - there is no expected behavior and anything can happen. – Evk May 17 '18 at 13:28
  • 1
    "Is that the expected behavior one would see from adding to a non-synchronized list in multiple threads?" The reason that there are data structures for multithreaded environments is because there is no consistent expected behavior for a data structure that is not thread safe. – Lithium May 17 '18 at 13:29
  • Can this `Id` be `null`? – Tim Schmelter May 17 '18 at 13:29
  • @TimSchmelter - does that really matter? – bommelding May 17 '18 at 13:34
  • 1
    @bommelding: if his question matters then this is a question that OP has to answer first, because `nextListItem` is the `Id` and OP wonders why it is `null`. – Tim Schmelter May 17 '18 at 13:35
  • Look at the [`Add`](https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,220) method. It consists of 3 lines and all three of them have potential issues if multiple threads are calling it simultaneously. – Damien_The_Unbeliever May 17 '18 at 13:39
  • Another question that arises is: do you also remove items from the list? – Tim Schmelter May 17 '18 at 13:42
  • @TimSchmelter no the id is never null. – Nicholas DiPiazza May 17 '18 at 14:11
  • Hi all I read that duplicate question and yes it's very related. but my quesiton in particular is - will that result in a null in the list? because i am fixing the bug telling them this will fix it but i have no way to prove that's definitely the real problem. – Nicholas DiPiazza May 17 '18 at 14:16

1 Answers1

0

The 'expected behaviour' is an exception and/or invalid data.

You only report null values but your somelist could also have duplicates and missing items.

Run it a little longer and you might see index-out-of-range errors, null reference exceptions etc.

bommelding
  • 2,969
  • 9
  • 14