10BC0 Interlocked.Increment sample does not raise memory barriers for static variables · Issue #4585 · dotnet/dotnet-api-docs · GitHub
[go: up one dir, main page]

Skip to content

Interlocked.Increment sample does not raise memory barriers for static variables #4585

@ohadschn

Description

@ohadschn

Docs page:
https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.increment?view=netcore-3.1

Relevant code section:

static int totalCount = 0;
static int totalMidpoint = 0;
static int midpointCount = 0;

These variables are accessed by different threads but only writes are protected (using Interlocked.Add / Interlocked.Increment).

However all reads should be protected too (otherwise different threads can read stale values). volatile would be the simplest way, but due to its complexity most prefer something like Interlocked.CompareExchange or even regular locks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0