Multi-threaded buffer access

S

ShaunO

I have a buffer (a byte array)
There will one thread writing data to the buffer and another emptying the
buffer.
To 'empty' the buffer it does a Buffer.BlockCopy to move the unread data to
the front of the array and update the data tail marker.

Should i use Monitor.Enter and Exit to ensure that the 2 threads dont clash
ie. one tries to read data from the buffer whilst the other is doing a
blockcopy on it ?

Thanks,
S
 
M

Marc Gravell

Unless you can guarantee that they are never going to try to look at the
same data, then yes: this will almost certainly need synchronization.
However, I wonder if a Queue<T> might be more suitable? Depends on
exactly what is happening, of course...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top