ConcurrentBag in C#: retarget net10.0, List<int> instead of ArrayList, Clear test - #2217
Open
vladimir-pecanac-main wants to merge 2 commits into
Open
vladimir-pecanac-main wants to merge 2 commits into
vladimir-pecanac-main wants to merge 2 commits into
Conversation
…, Clear test Retarget both projects from net7.0 to net10.0 and bump the test packages (Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1). Replace the non-generic ArrayList with List<int> in RemoveFromConcurrentBag, RemoveFromConcurrentBagConcurrently and AccessItemFromAConcurrentBag, and update the three tests that asserted the ArrayList type. Drop the dead array allocation in ConcurrentBagToArrayMethod, split CreateConcurrentBag into the three construction forms it was demonstrating with its locals removed, document the Count-then-CopyTo race in ConcurrentBagCopyToMethod, convert Program.cs to top-level statements that print the count and the empty flag, and add the missing Clear test. Build: 0 warnings, 0 errors. Tests: 8 of 8 passing on SDK 10.0.302, runtime 10.0.10.
… a dedicated thread DrainOwnQueue fills a bag and drains it on the same thread, which returns the items last in, first out. DrainStolenQueue fills the bag on one dedicated thread and drains it on another, so the consumer steals from the head of the producer's queue and the items come back oldest first. Both use dedicated Thread objects with Join(), never Task.Run: the pool is free to hand the producer and the consumer to the same thread, which makes the consumer pop its own queue LIFO instead of stealing and the assertion intermittently red. Two tests added. The suite runs 10 of 10 on five consecutive runs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Retargets
collections-csharp/ConcurrentBagInCSharpto .NET 10 and modernises the sample that backs the ConcurrentBag article.net7.0tonet10.0.Microsoft.NET.Test.Sdk17.3.2 to 18.10.1,xunit2.4.2 to 2.9.3,xunit.runner.visualstudio2.4.5 to 4.0.0,coverlet.collector3.1.2 to 10.0.1 (versions re-queried on NuGet today).ArrayListreplaced withList<int>inRemoveFromConcurrentBag,RemoveFromConcurrentBagConcurrentlyandAccessItemFromAConcurrentBag, with the three matching tests updated.ConcurrentBagToArrayMethodno longer allocates an array it immediately throws away.CreateConcurrentBagsplit into the three construction forms it was demonstrating, with its unused locals removed.ConcurrentBagCopyToMethodkeeps its behaviour and gains a comment explaining thatCountandCopyToare two operations, so the destination has to be sized from a snapshot when other threads can still write.Program.csconverted to top-level statements that actually print the count and the empty flag.ConcurrentBagClearMethod, the one public method the suite did not cover.Build: 0 warnings, 0 errors. Tests: 8 of 8 passing on SDK 10.0.302, runtime 10.0.10.