Skip to content

Atomic<T> for primitive types in .NET #114242

@FlorentBunjaku1

Description

@FlorentBunjaku1

Is there any kind of type built in type that removes the need to manually handle the thread-safety of primitive types, if not could it be possible to have a generic class implementation like the following

Proposed API

public class Atomic<T> where T : struct
{
    public T Value { get; }

    public void Update(T value);

    public void Update(Func<T, T> updateFunc);
}

Example Usage

var counter = new Atomic<int>(0);

counter.Update(5);

counter.Update(x => x + 1);

Console.WriteLine(counter.Value); 

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions