I implemented a Pipe class that internally uses BlockingQueue to store the received data.
There are two situations where BlockingQueue blocks the calling thread:
- The calling thread calls
Dequeue() and the queue is empty. It will block the thread until an element is found. - The calling thread calls
Enqueue() and the queue is full. It will block the stream until there is again room for inserting data.
My initial idea was that instead of the Pipe class creating an instance of BlockingQueue , I passed it an IQueue instance by installing the constructor. That way, when testing, I would give him an instance of NonBlockingQueue , so I would not have to worry about problems with threads (when I run Unit tests for both the Pipe class and other classes that use Pipes I would just like to add things in line and do not think if they are already full and the like).
The problem is that in doing so, I actually make my Pipe behave differently, depending on the type of IQueue instance that I pass to it:
In BlockingQueue , if the queue is empty and you are trying to recover something from it, it will be blocked until it receives something. In NonBlockingQueue it will just throw an exception.
In BlockingQueue , if the queue is full and you are trying to add something, it will wait until someone removes the item and a space appears again. The NonBlockingQueue version NonBlockingQueue either FullQueueException or allow an "infinite" number of elements.
That is, there is no single agreement. I think this approach is definitely wrong.
What is a more suitable approach to this?
Change to Mitch:
This is used to implement the Pipe & Filter system: each filter has input and output channels. Then each filter is implemented with a form code.
char c; while ((c = inputPipe.ReadChar()) != STREAM_TERMINATOR) {
so I think, yes, blocking pipes / queues is the behavior I want.
source share