Skip to content

Add warnings and guidance against using volatile keyword #47282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 10, 2025

Fixes #23832

The volatile keyword documentation now includes prominent warnings about its limitations and strongly recommends safer alternatives for multithreaded programming.

Changes Made

  • Added prominent CAUTION block warning against misuse of volatile early in the document
  • Created new "Alternatives to volatile" section explaining safer options:
    • System.Threading.Interlocked operations for atomic operations
    • lock statements for mutual exclusion
    • System.Threading.Volatile class for explicit volatile operations
    • Higher-level synchronization primitives and concurrent collections
  • Enhanced existing content to emphasize safer alternatives throughout the document
  • Expanded "See also" section with modern threading resources

Why These Changes

The C# community has identified that volatile is often misunderstood and misused, leading to subtle concurrency bugs. The existing documentation provided technical information but lacked guidance about when not to use volatile and what to use instead.

What This Addresses

  • Warns developers about common misconceptions with volatile
  • Explains that volatile doesn't provide atomicity, prevent race conditions, or guarantee memory ordering
  • Directs developers toward modern, safer concurrency tools
  • Maintains all existing technical content for those who need to understand volatile

The documentation now follows the community consensus that volatile should be avoided in favor of more reliable alternatives while still providing the technical details for advanced scenarios.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@BillWagner
Copy link
Member

@copilot Can you start working on this again?

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Include warnings against using volatile Add warnings and guidance against using volatile keyword Jul 10, 2025
Copilot finished work on behalf of BillWagner July 10, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc lang-reference/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include warnings against using volatile
2 participants