linear_ringbuffer icon indicating copy to clipboard operation
linear_ringbuffer copied to clipboard

thread safe io_buffer

Open mipac opened this issue 5 years ago • 2 comments

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

mipac avatar Dec 18 '19 14:12 mipac

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.

lava avatar Dec 18 '19 16:12 lava

ok thank you for reply

Le mer. 18 déc. 2019 à 17:47, Benno Evers [email protected] a écrit :

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

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

// Writer thread { std::lock_guardstd::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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lava/linear_ringbuffer/issues/1?email_source=notifications&email_token=ABMPSA52YFLOVFICOECP3X3QZJHZZA5CNFSM4J4LVNIKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHGX7LQ#issuecomment-567115694, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMPSA5NBKLLMSN5FYFTQYTQZJHZZANCNFSM4J4LVNIA .

mipac avatar Dec 18 '19 21:12 mipac