I am trying to write a buffermanager that controls 3 threads. Typical use will be with a slow producer and fast consumer. The idea of ββthe three buffers is that the producer ALWAYS has a buffer for writing, and the consumer ALWAYS receives the latest data.
Now I already have this, and this sorting works.
namespace YariIfStream
{
public class YariIFStream
{
private Stream writebuf;
private Stream readbuf;
private Stream swapbuf;
private bool firsttime;
private Object sync;
public YariIFStream()
{
sync = new Object();
eerste = true;
writebuf = new MemoryStream();
readbuf = new MemoryStream();
swapbuf = new MemoryStream();
}
public Stream GetReadBuffer()
{
lock (sync)
{
Monitor.Wait(sync);
Stream tempbuf = swapbuf;
swapbuf = readbuf;
readbuf = tempbuf;
}
return readbuf;
}
public Stream GetWriteBuffer()
{
lock (sync)
{
Stream tempbuf = swapbuf;
swapbuf = writebuf;
writebuf = tempbuf;
if (!firsttime)
{
Monitor.Pulse(sync);
}
else
{
firsttime = false;
}
}
return writebuf;
}
}
}
The first check is used because the first time a write request is given, it cannot impulse the user, because the buffer still needs to be written with data. When the write buffer is written a second time, we can be sure that the previous buffer contains data.
I have two streams, one producer and one consumer. This is my conclusion:
prod: uv_hjd`alv cons: N/<]g[)8fV
prod: N/<]g[)8fV cons: 5Ud*tJ-Qkv
prod: 5Ud*tJ-Qkv cons: 4Lx&Z7qqjA
prod: 4Lx&Z7qqjA cons: kjUuVyCa.B
prod: kjUuVyCa.B
, , .
, , .
:
- , . , -...
- Thread.Sleep(1); GetWriteBuffer() . - .
.