Skip to content

Add race condition examples to PLINQ shared memory pitfalls section#51523

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/add-examples-for-race-condition
Draft

Add race condition examples to PLINQ shared memory pitfalls section#51523
Copilot wants to merge 4 commits intomainfrom
copilot/add-examples-for-race-condition

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

The "Avoid writing to shared memory locations" section in the PLINQ documentation lacked concrete examples demonstrating race conditions.

Changes

  • Added race condition example showing unsafe parallel writes to shared variable producing incorrect results
  • Added correct approach using thread-safe Sum() aggregation
  • Created compilable snippets in both C# and VB with project files
  • Enhanced explanation of why total += n fails (non-atomic read-modify-write causing lost updates)

Example

// UNSAFE: Race condition
int total = 0;
numbers.AsParallel().ForAll(n => total += n);
// Result: unpredictable, less than expected 49,995,000

// SAFE: Thread-safe aggregation
int total = numbers.AsParallel().Sum();
// Result: always 49,995,000

The documentation now references Aggregate() and ConcurrentBag<T> as alternative safe patterns.

Original prompt

This section details on the original issue you should resolve

<issue_title>Consider adding more examples</issue_title>
<issue_description>Specially under the "Avoid writing to shared memory locations". An example that shows a race condition would be really good to understand this concept.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)

@dotnet-bot This issue has been closed as part of the issue backlog grooming process outlined in #22351.

That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.</comment_new>
<comment_new>@Youssef1313
@BillWagner Consider re-opening this one.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/standard/parallel-programming/potential-pitfalls-with-plinq.md docs/standard/parallel-programming/potential-pitfalls-with-plinq

Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
Copilot AI and others added 2 commits February 5, 2026 00:24
Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
Copilot AI changed the title [WIP] Add examples for race condition in shared memory section Add race condition examples to PLINQ shared memory pitfalls section Feb 5, 2026
Copilot AI requested a review from meaghanlewis February 5, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider adding more examples

2 participants