Skip to content

Document thread safety issues and provide example#369

Open
agocke wants to merge 1 commit into
dotnet:mainfrom
agocke:main
Open

Document thread safety issues and provide example#369
agocke wants to merge 1 commit into
dotnet:mainfrom
agocke:main

Conversation

@agocke
Copy link
Copy Markdown
Member

@agocke agocke commented May 28, 2026

Added a note on memory safety violations due to data races and provided an example of a NativeBuffer class that illustrates potential issues with concurrent access.

Added a note on memory safety violations due to data races and provided an example of a NativeBuffer class that illustrates potential issues with concurrent access.
}
```

The above code can produce memory safety violations in the presence of data races. Consider two threads, one that invokes `Dispose` and the other that invokes `ReadAt` simultaneously. If the first thread executes right between the `_ptr[index]` derefence, the dereference will be invalid (out of bounds/use after free). Both the `Dispose` method and `ReadAt` method would need to be marked `unsafe` to correctly communicate the requirement that they must not be executed concurrently, or the implementation would have to be rewritten to use synchronization.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking Dispose method as unsafe is not an option since it implements safe interface method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants