Skip to content

ConcurrentBag in C#: retarget net10.0, List<int> instead of ArrayList, Clear test - #2217

Open
vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/78873-csharp-concurrentbag
Open

vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/78873-csharp-concurrentbag

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Retargets collections-csharp/ConcurrentBagInCSharp to .NET 10 and modernises the sample that backs the ConcurrentBag article.

  • Both projects move from net7.0 to net10.0.
  • Test packages: Microsoft.NET.Test.Sdk 17.3.2 to 18.10.1, xunit 2.4.2 to 2.9.3, xunit.runner.visualstudio 2.4.5 to 4.0.0, coverlet.collector 3.1.2 to 10.0.1 (versions re-queried on NuGet today).
  • ArrayList replaced with List<int> in RemoveFromConcurrentBag, RemoveFromConcurrentBagConcurrently and AccessItemFromAConcurrentBag, with the three matching tests updated.
  • ConcurrentBagToArrayMethod no longer allocates an array it immediately throws away.
  • CreateConcurrentBag split into the three construction forms it was demonstrating, with its unused locals removed.
  • ConcurrentBagCopyToMethod keeps its behaviour and gains a comment explaining that Count and CopyTo are two operations, so the destination has to be sized from a snapshot when other threads can still write.
  • Program.cs converted to top-level statements that actually print the count and the empty flag.
  • New test for 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.

…, 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant