J'essaie d'implémenter un simple tampon circulaire.Mon tampon circulaire simple ne fonctionne pas correctement
class MyCircularBuffer
{
byte[] Buffer = null;
private Object BufferLock = new object();
int readIndex = 0;
int writeIndex = 0;
int SpaceBetweenIndexes
{
get;
set;
}
int ReadIndex
{
get
{
return readIndex;
}
set
{
readIndex = value;
CalculateSpaceBetweenIndexes();
}
}
void CalculateSpaceBetweenIndexes()
{
if (readIndex <= writeIndex)
{
SpaceBetweenIndexes = writeIndex - readIndex;
}
else
{
SpaceBetweenIndexes = Length - (readIndex - writeIndex);
}
}
int WriteIndex
{
get
{
return writeIndex;
}
set
{
writeIndex = value;
CalculateSpaceBetweenIndexes();
}
}
int Length;
int Count
{
get
{
lock(BufferLock)
{
return Buffer.Count();
}
}
}
public MyCircularBuffer(int length)
{
Buffer = new byte[length];
this.Length = length;
ReadIndex = 0;
WriteIndex = 0;
}
public byte ReadByte()
{
while (SpaceBetweenIndexes <= 1)
{
CalculateSpaceBetweenIndexes();
}
ReadIndex++;
if (ReadIndex < Length)
{
Console.WriteLine("Read from index " + (ReadIndex-1) + " value " + this[ReadIndex - 1]);
return this[ReadIndex - 1];
}
else
{
ReadIndex = 0;
Console.WriteLine("Read from index " + (Length-1) + " value " + this[Length-1]);
return this[Length-1];
}
}
public void WriteByte(byte value)
{
WriteIndex++;
if (WriteIndex < Length)
{
Console.WriteLine("Wrote from index " + (WriteIndex-1) + " value " + value);
this[WriteIndex - 1] = value;
}
else if(writeIndex == Length)
{
Console.WriteLine("Wrote from index " + (WriteIndex-1) + " value " + value);
this[WriteIndex-1] = value;
WriteIndex = 0;
}
}
public byte this[int index]
{
get
{
lock(BufferLock)
{
return Buffer[index];
}
}
set
{
lock(BufferLock)
{
Buffer[index] = value;
}
}
}
}
code pour générer le test:
class Program
{
public static MyCircularBuffer buff = new MyCircularBuffer(10);
static void Main(string[] args)
{
System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(DrawByte));
System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(WriteByte));
t2.Start();
t1.Start();
Console.ReadKey();
}
public static void DrawByte()
{
Random rand = new Random(50);
for (int i = 0; i < 20; i++)
{
buff.WriteByte((byte)(rand.Next()+1));
}
}
public static void WriteByte()
{
for (int i = 0; i < 20; i++)
{
buff.ReadByte(); //In this example my class is showing the results, so I didn't write here Console.Writeline...
}
}
}
fil d'abord utilisera la méthode ReadByte pour obtenir octets de ce tampon et le second fil utilisera la méthode WriteByte pour les octets d'écriture dans la mémoire tampon. J'ai writeindex et readindex qui se déplacent sur le buffer. Tout semble fonctionne bien, mais ceci:
a bug http://i62.tinypic.com/2d9tmrd.png
Toutes les idées/conseils? J'espère que vous pouvez m'aider, les gars!
Vous devriez peut-être ajouter le code pour générer le cas de test, de sorte que les personnes répondant à la question puissent tester leur solution. –
Vous devriez ajouter un verrouillage autour des méthodes 'readByte()' et 'writeByte()' – Domysee
Que se passe-t-il lorsqu'un lecteur est à la traîne de l'auteur ou vice versa? Devraient-ils attendre les uns les autres? –