r/programming • • Oct 11 '26

[ Removed by moderator ]

/r/programming/comments/1x3b0dy/removed_by_moderator/

[removed] — view removed post

0 Upvotes

4 comments sorted by

1

u/yanitrix Oct 11 '26

wouldn't you get the same fix by replacing Task.WhenAll with foreach (shark in sharks) await Attack(shark, prey)

2

u/Coding-Mojo Oct 11 '26

That's a good question. You would for sure prevent the resource modification concurrency this way, but you'ld fall into a performance issue then.

In this exemple, each shark need X seconds to process his attack. Awaiting would not send "all sharks at the same time", but instead "one after another".

So let's say each shark need 5 seconds to process it's attack, and there is 100 sharks, the process would take 5 x 100 = ~8min

By sending all sharks at once using Task.WhenAll, attacks are "parallelised", so the max processing delay will be equal to the slowest of all sharks.

1

u/yanitrix Oct 11 '26

ah, it makes sense now. Thanks

1

u/EliSka93 Oct 11 '26

I think the goals could be read in a way where this is true tbh

In a real program though you probably want the async to actually run asynchronous.