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

thread safe io_buffer #1

Open
mipac opened this issue Dec 18, 2019 · 2 comments
Open

thread safe io_buffer #1

mipac opened this issue Dec 18, 2019 · 2 comments

Comments

@mipac
Copy link

mipac commented Dec 18, 2019

I'd like to use io_buffer from a reader thread and a writer thread
How can I do it safely and efficiently?

@lava
Copy link
Owner

lava commented Dec 18, 2019

The only way I see is to mutex the whole buffer, i.e.

// Reader thread
{ std::lock_guard<std::mutex> lock(io_mutex);
  io_buffer.read(...);
}

// Writer thread
{ std::lock_guard<std::mutex> lock(io_mutex);
  io_buffer.write(...);
}

Even if you're willing to patch the io_buffer to allow finer-grained locking, I don't really see a good way to implement this. The problem is the resetting of the read/write position when the buffer is completely empty, so both threads really need to be able to change the write position.

Of course, an alternative would be to just use the linear_ringbuffer instead, which supports that use case natively.

@mipac
Copy link
Author

mipac commented Dec 18, 2019 via email

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

2 participants