Skip to content

Refactor: Replace ThreadStatic with ThreadLocal for Thread Safety#2

Draft
gartia wants to merge 1 commit intoFacepunch:masterfrom
gartia:master
Draft

Refactor: Replace ThreadStatic with ThreadLocal for Thread Safety#2
gartia wants to merge 1 commit intoFacepunch:masterfrom
gartia:master

Conversation

@gartia
Copy link
Copy Markdown

@gartia gartia commented Apr 28, 2024

The existing use of ThreadStatic for staticBuffer effectively makes it a global static variable across all threads, with initialization in the static constructor. This approach can lead to issues where multiple threads overwrite each other's data, as each thread does not get its own instance of the buffer.

ThreadLocal<T>, on the other hand, ensures that each thread gets its own instance of the buffer, initialized independently. This change will enhance the reliability of serialization when ProtocolParser is used concurrently across multiple threads.

Using this method you are required to use staticBuffer.Value though if any of the non public code is using staticBuffer it will need to be changed.

…afety

The existing use of `ThreadStatic` for `staticBuffer` effectively makes it a global static variable across all threads, with initialization in the static constructor. This approach can lead to issues where multiple threads overwrite each other's data, as each thread does not get its own instance of the buffer.

`ThreadLocal<T>`, on the other hand, ensures that each thread gets its own instance of the buffer, initialized independently. This change will enhance the reliability of serialization when `ProtocolParser` is used concurrently across multiple threads.

Using this method you are required to use staticBuffer.Value though if any of the non public code is using staticBuffer it will need to be changed.
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.

1 participant