Queue in bytes with asynchronous stream

I have a callback method that is called whenever new data is available:

public delegate void DataCallback( byte[] buffer, int offset, int count); 

I want to wrap this in a class that implements an interface similar to this:

 public interface IDataSource { IAsyncResult BeginRead( byte[] buffer, int offset, int size, TimeSpan timeout, AsyncCallback callback, object state); int EndRead( IAsyncResult asyncResult); int Read( byte[] buffer, int offset, int size, TimeSpan timeout); } 

This is obviously the classic problem of consumer producers: bytes are produced by calls to the callback method and consumed by the Begin / EndRead and Read methods. The Begin / EndRead and Read methods should be blocked if data is not available (until a timeout occurs). The implementation must use a fixed-size internal buffer, so the callback method should block when the buffer is currently full.

Since thinking about multithreading usually leads to a serious headache, my question is: Is there already an implementation of such a data structure?

(I think the implementation of the Read method should be fairly simple, but I would like to avoid implementing the Begin / EndRead with Read. Begin / EndInvoke .)

+3
multithreading c # queue producer-consumer
source share
2 answers

Should it be asynchronous via IAsyncResult ? I have a common blocking queue here (i.e., Readers are blocked until there is no data or they are closed, authors block until there is a space); it is not optimized specifically for byte[] , but so far the size is not huge, it must handle it - but as a blocking queue it needs (at least one) dedicated consumer stream, doing:

 T val; while(queue.TryDequeue(out val)) { // process val } 
+1
source share

I think you should do a google search in "lockless queue". So I have a lot of useful hits.

0
source share

All Articles