我试图撰写一个管理3台电梯的缓冲器。 典型的使用方式是生产者缓慢,消费者快。 三个缓冲带背后的想法是,ALWAYS的生产者有一个缓冲,可以向消费者协会提供最新数据。
现在我已经这样做了,而且它有分寸。
namespace YariIfStream
{
/// <summary>
/// A class that manages three buffers used for IF data streams
/// </summary>
public class YariIFStream
{
private Stream writebuf; ///<value>The stream used for writing</value>
private Stream readbuf; ///<value>The stream used for reading</value>
private Stream swapbuf; ///<value>The stream used for swapping</value>
private bool firsttime; ///<value>Boolean used for checking if it is the first time a writebuffers is asked</value>
private Object sync; ///<value>Object used for syncing</value>
/// <summary>
/// Initializes a new instance of the Yari.YariIFStream class with expandable buffers
/// </summary>
public YariIFStream()
{
sync = new Object();
eerste = true;
writebuf = new MemoryStream();
readbuf = new MemoryStream();
swapbuf = new MemoryStream();
}
/// <summary>
/// Returns the stream with the buffer with new data ready to be read
/// </summary>
/// <returns>Stream</returns>
public Stream GetReadBuffer()
{
lock (sync)
{
Monitor.Wait(sync);
Stream tempbuf = swapbuf;
swapbuf = readbuf;
readbuf = tempbuf;
}
return readbuf;
}
/// <summary>
/// Returns the stream with the buffer ready to be written with data
/// </summary>
/// <returns>Stream</returns>
public Stream GetWriteBuffer()
{
lock (sync)
{
Stream tempbuf = swapbuf;
swapbuf = writebuf;
writebuf = tempbuf;
if (!firsttime)
{
Monitor.Pulse(sync);
}
else
{
firsttime = false;
}
}
//Thread.Sleep(1);
return writebuf;
}
}
}
第一次检查之所以使用,是因为第一次请购书,它不能冲动消费者,因为缓冲仍然必须用数据书写。 当第二次要求笔记时,我们可以确保以前的缓冲包含数据。
I have two threads, one producer and one consumer. This is my output:
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
Now it s ok the consumer lags one behind, it is supposed to do that. As you can see i lose my first string of data wich is my main problem.
其他问题是:
- if i remove the firsttime check, it works. But it shouldn t in my opinion...
- if i add a Thread.Sleep(1); in the GetWriteBuffer() it also works. Something i don t understand.
事先感谢任何启迪。