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

Is ByteBuffer thread-safe? #166

Closed
dirk68-fu opened this issue Mar 15, 2018 · 2 comments
Closed

Is ByteBuffer thread-safe? #166

dirk68-fu opened this issue Mar 15, 2018 · 2 comments

Comments

@dirk68-fu
Copy link

dirk68-fu commented Mar 15, 2018

Sorry, if this question is dumb, because the whole purpose of the object may be to allow exactly this, but the documentation does not specifically clarify this.

Is it safe to write from one thread and read from another?

@Lukasa
Copy link
Contributor

Lukasa commented Mar 15, 2018

Yes, but it won’t do what you want.

ByteBuffer is a copy on write value-type data structure. If you are writing and reading from multiple threads, you necessarily have multiple references to the underlying buffer. Thus, the reading thread will not see the writes from the writing thread.

In general, however, it is recommended that you don’t do this: it only makes sense if you conceptualise of ByteBuffer as a reference type, which is not the right way to think about it.

@Lukasa Lukasa closed this as completed Mar 15, 2018
@normanmaurer
Copy link
Member

@dirk68-fu just to clarify its still "valid" to write to the ByteBuffer from one thread, pass it to another thread and read from it tho. Basically the reading side will see a "snapshot" of the written data.

weissi added a commit to weissi/swift-nio that referenced this issue Jun 13, 2020
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

No branches or pull requests

3 participants