MemoryStream 有一个线程写入它,另一个线程读取它

问题描述 投票:0回答:2

这就是我写入流然后使用 1 个线程从中读取的方式:

        System.IO.MemoryStream ms = new System.IO.MemoryStream();

        // write to it
        ms.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7 }, 0, 7);

        // go to the begining
        ms.Seek(0, System.IO.SeekOrigin.Begin);

        // now read from it
        byte[] myBuffer = new byte[7];
        ms.Read(myBuffer, 0, 7);

现在我想知道是否可以从一个线程写入内存流并从另一个线程读取该流。

c# multithreading stream memorystream
2个回答
19
投票

您不能同时使用从 2 个线程寻求功能的 Stream,因为 Stream 状态已满。例如NetworkStream 有 2 个通道,一个用于读取,一个用于写入,因此不支持搜索。

如果需要seeking capabilities,需要创建2个streams,一个读一个写。否则,您可以简单地创建一个新的 Stream 类型,该类型允许通过对底层流进行独占访问并恢复其写入/读取位置来从底层内存流读取和写入。一个原始的例子是:

class ProducerConsumerStream : Stream
{
    private readonly MemoryStream innerStream;
    private long readPosition;
    private long writePosition;

    public ProducerConsumerStream()
    {
        innerStream = new MemoryStream();
    }

    public override bool CanRead { get { return true;  } }

    public override bool CanSeek { get { return false; } }

    public override bool CanWrite { get { return true; } }

    public override void Flush()
    {
        lock (innerStream)
        {
            innerStream.Flush();
        }
    }

    public override long Length
    {
        get 
        {
            lock (innerStream)
            {
                return innerStream.Length;
            }
        }
    }

    public override long Position
    {
        get { throw new NotSupportedException(); }
        set { throw new NotSupportedException(); }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        lock (innerStream)
        {
            innerStream.Position = readPosition;
            int red = innerStream.Read(buffer, offset, count);
            readPosition = innerStream.Position;

            return red;
        }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        throw new NotSupportedException();
    }

    public override void SetLength(long value)
    {
        throw new NotImplementedException();
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        lock (innerStream)
        {
            innerStream.Position = writePosition;
            innerStream.Write(buffer, offset, count);
            writePosition = innerStream.Position;
        }
    }
}

0
投票

如果有人仍在寻找解决方案,下面的代码非常简单,似乎可以很好地同时从两个不同的线程读取/写入。

public class RWMemoryStream : MemoryStream
{
    private long readPosition;
    private long writePosition;

    private readonly object lockStream = new object();

    public override void Write(byte[] buffer, int offset, int count)
    {
        lock (lockStream)
        {
            Position = writePosition;
            base.Write(buffer, offset, count);
            writePosition = Position;
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int result;
        
        lock (lockStream)
        {
            Position = readPosition;
            result = base.Read(buffer, offset, count);
            readPosition += result;
        }

        return result;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.