[API Proposal]: `Interlocked.Read` generic API

Open
#107,441 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
api

Research direction

Start at the System.Threading.Interlocked API entry point and review the existing Read overloads alongside the proposed generic signature. Determine the supported type and atomicity requirements, then document whether the API proposal is acceptable and what tests would demonstrate the behavior shown in the usage example.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Threading
Background and motivation

In additional to #64658 & #65184 should we consider introduce an API for Interlocked.Read<T>(ref T field)

API Proposal
namespace System.Threading;

public static class Interlocked
{
+    public static T Read<T>(ref T location);
}
API Usage
var progress = 1;

Task.Run(() => 
{
    while(true)
    {
      var value = Interlocked.Read(ref progress);
      Console.WriteLine(value);
        
      if (value >= 100)
          break;
        
      Thread.Sleep(1000);
    }
});

Parallel.For(1, 100, (i, _) => 
{

  // ...
  Thread.Sleep(i * 30);
  Interlocked.Increment(ref progress);
});

Console.WriteLine("done");
Console.ReadLine();

Alternative Designs

No response

Risks

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.