HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Read and write in C# buffer in parallel

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
readwriteparallelandbuffer

Problem

I have a buffer - int array with defined length. I want to read and write values into this array in parallel. For instance, 5 readers and 3 writers with different intervals. Additionally, I want to block waiting readers and waiting writers until a specific operation (read or write) is available.

Buffer:

public class IntBuffer
{
    private readonly AutoResetEvent _readDone = new AutoResetEvent(false);
    private readonly AutoResetEvent _writeDone = new AutoResetEvent(false);

    private readonly int[] _array;
    private int _index;

    public IntBuffer(int bufferSize)
    {
        _array = new int[bufferSize];
        _index = -1;
    }

    public void Write(int insert)
    {
        while (true)
        {
            if (_index >= -1 && _index = -1 && _index = 0)
            {
                try
                {
                    Monitor.Enter(this);
                    if (_index >= 0)
                    {
                        var val = _array[_index--];
                        return val;
                    }
                }
                finally
                {
                    Monitor.Exit(this);
                    _writeDone.Set();
                }
            }

            Console.WriteLine("waiting for read...");
            _readDone.WaitOne();
        }
    }
}


Reader:

public class Reader
{
    private readonly IntBuffer _buff;

    public Reader(IntBuffer buff)
    {
        _buff = buff;
    }

    public void Start(int sleepMs)
    {
        while (true)
        {
            Thread.Sleep(sleepMs);
            Console.WriteLine($"reader read {_buff.Read()}");
        }
    }
}


Writer:

```
public class Writer
{
private readonly IntBuffer _buff;
private readonly Random _random = new Random(Guid.NewGuid().GetHashCode());

public Writer(IntBuffer buff)
{
_buff = buff;
}

public void Start(int sleepMs)
{
while (true)
{
Thread.Sleep(sleepMs);

Solution

What you are trying to archive is very similar to the BlockingCollection. Note that you can look at the .net framework's source code to get an idea how to implement it professionally ;).

The overall implementation seems to work and I think the blocking mechanism does what it is supposed to.

However, some points to note:

  • Do not use the instance of the buffer as synchronization object (Monitor.Enter(this);) because the object can be locked by someone else from outside... Create a private sync object instead.



  • In method Read, the temporary variable val is not required - just return the value.



  • I would rename the class to make clear, that the call of Write may block the current thread (maybe BlockingBuffer, BlockingStack or something like that).



  • Why not making the class generic?

Context

StackExchange Code Review Q#146328, answer score: 2

Revisions (0)

No revisions yet.