Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace ThreadStatic with ThreadLocal for Thread Safety #2

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gartia
Copy link

@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
1 participant