.NET的线程安全缓冲区

时间:2012-05-08 15:10:26

标签: c# .net-3.5 thread-safety buffer stringbuilder

(注意:虽然我想了解.Net 4.0的未来想法,但我只限于此项目的.Net 3.5。)

我有一个线程,它从外部设备异步读取数据(在代码示例中通过创造性的strSomeData :-)模拟并将其存储在StringBuilder'缓冲区'中(strBuilderBuffer: - )

在'主要代码'中我想在这个'缓冲区'''唠叨'。但是,从“操作”的角度来看,我不确定如何以线程安全的方式执行此操作。我理解从“数据”的角度看它是安全的,因为根据msdn,“这个(StringBuilder)类型的任何公共静态成员都是线程安全的。任何实例成员都不能保证是线程安全的。”但是,下面的代码说明从“运营”角度来看,它可能不是线程安全的。

关键是我担心两行代码:

string strCurrentBuffer = ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.ToString();
// Thread 'randomly' slept due to 'inconvenient' comp resource scheduling...
ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.Length = 0;

如果计算机操作系统在缓冲区的“读取”和“读取”之间休眠。缓冲区的'清除',我可以丢失数据(这很糟糕: - (

有没有办法保证'atomocy?'这两条线和强迫计算机不要打断它们?

关于弗拉德下面关于使用lock的建议,我尝试了但是它没有用(实际上是真的):

    public void BufferAnalyze()
    {
        String strCurrentBuffer;
        lock (ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer)
        {
            strCurrentBuffer = ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.ToString();
            Console.WriteLine("[BufferAnalyze()]  ||<<  Thread 'Randomly' Slept due to comp resource scheduling");
            Thread.Sleep(1000);  //  Simulate poor timing of thread resourcing...
            ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.Length = 0;
        }
        Console.WriteLine("[BufferAnalyze()]\r\nstrCurrentBuffer[{0}] == {1}", strCurrentBuffer.Length.ToString(), strCurrentBuffer);
    }

有没有更好的方法来实现线程安全缓冲区?

这是完整的代码:

namespace ExploringThreads
{
    /// <summary>
    /// Description of BasicThreads_TestThreadSafety_v1a
    /// </summary>
    class ThreadWorker_TestThreadSafety_v1a
    {
        private Thread thread;
        public static StringBuilder strBuilderBuffer = new StringBuilder("", 7500);
        public static StringBuilder strBuilderLog = new StringBuilder("", 7500);

        public bool IsAlive
        {
            get { return thread.IsAlive; }
        }

        public ThreadWorker_TestThreadSafety_v1a(string strThreadName)
        {
            // It is possible to have a thread begin execution as soon as it is created.
            // In the case of MyThread this is done by instantiating a Thread object inside MyThread's constructor.
            thread = new Thread(new ThreadStart(this.threadRunMethod));
            thread.Name = strThreadName;
            thread.Start();
        }

        public ThreadWorker_TestThreadSafety_v1a() : this("")
        {
            //   NOTE:  constructor overloading ^|^
        }

        //Entry point of thread.
        public void threadRunMethod()
        {
            Console.WriteLine("[ThreadWorker_TestThreadSafety_v1a threadRunMethod()]");
            Console.WriteLine(thread.Name + " starting.");
            int intSomeCounter = 0;
            string strSomeData = "";
            do
            {
                Console.WriteLine("[ThreadWorker_TestThreadSafety_v1a threadRunMethod()] running.");
                intSomeCounter++;
                strSomeData = "abcdef" + intSomeCounter.ToString() + "|||";
                strBuilderBuffer.Append(strSomeData);
                strBuilderLog.Append(strSomeData);
                Thread.Sleep(200);
            } while(intSomeCounter < 15);

            Console.WriteLine(thread.Name + " terminating.");
        }
    }
    /// <summary>
    /// Description of BasicThreads_TestThreadSafety_v1a.
    /// </summary>
    public class BasicThreads_TestThreadSafety_v1a
    {
        public BasicThreads_TestThreadSafety_v1a()
        {
        }

        public void BufferAnalyze()
        {
            string strCurrentBuffer = ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.ToString();
            Console.WriteLine("[BufferAnalyze()]  ||<<  Thread 'Randomly' Slept due to comp resource scheduling");
            Thread.Sleep(1000);  //  Simulate poor timing of thread resourcing...
            ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.Length = 0;
            Console.WriteLine("[BufferAnalyze()]\r\nstrCurrentBuffer[{0}] == {1}", strCurrentBuffer.Length.ToString(), strCurrentBuffer);
        }

        public void TestBasicThreads_TestThreadSafety_v1a()
        {
            Console.Write("Starting TestBasicThreads_TestThreadSafety_v1a  >>>  Press any key to continue . . . ");
            Console.Read();

            // First, construct a MyThread object.
            ThreadWorker_TestThreadSafety_v1a threadWorker_TestThreadSafety_v1a = new ThreadWorker_TestThreadSafety_v1a("threadWorker_TestThreadSafety_v1a Child");

            do
            {
                Console.WriteLine("[TestBasicThreads_TestThreadSafety_v1a()]");
                Thread.Sleep(750);
                BufferAnalyze();
                //} while (ThreadWorker_TestThreadSafety_v1a.thread.IsAlive);
            } while (threadWorker_TestThreadSafety_v1a.IsAlive);
            BufferAnalyze();
            Thread.Sleep(1250);
            Console.WriteLine("[TestBasicThreads_TestThreadSafety_v1a()]");
            Console.WriteLine("ThreadWorker_TestThreadSafety_v1a.strBuilderLog[{0}] == {1}", ThreadWorker_TestThreadSafety_v1a.strBuilderLog.Length.ToString(), ThreadWorker_TestThreadSafety_v1a.strBuilderLog);

            Console.Write("Completed TestBasicThreads_TestThreadSafety_v1a  >>>  Press any key to continue . . . ");
            Console.Read();
        }
    }
}

3 个答案:

答案 0 :(得分:4)

下载3.5 Reactive Extensionshere后端。还有一个NuGet包。下载后,只需在项目中引用System.Threading.dll。

现在,您可以在.NET 3.5中使用.NET 4.0中的所有新并发集合标准。最适合您情况的是BlockingCollection。它基本上是一个缓冲区,允许线程将项排入队列并将它们像普通队列一样出列。除了出列操作阻止,直到某个项目可用。

现在根本不需要使用StringBuilder课程。这是我如何重构你的代码。我试图让我的例子简短,以便更容易理解。

public class Example
{
  private BlockingCollection<string> buffer = new BlockingCollection<string>();

  public Example()
  {
    new Thread(ReadFromExternalDevice).Start();
    new Thread(BufferAnalyze).Start();
  }

  private void ReadFromExteneralDevice()
  {
    while (true)
    {
      string data = GetFromExternalDevice();
      buffer.Add(data);
      Thread.Sleep(200);
    }
  }

  private void BufferAnalyze()
  {
    while (true)
    {
      string data = buffer.Take(); // This blocks if nothing is in the queue.
      Console.WriteLine(data);
    }
  } 
}

为了将来参考,TPL Data Flow库中的BufferBlock<T>类与BlockingCollection基本相同。它将在.NET 4.5中提供。

答案 1 :(得分:3)

使用StringBuffer不是线程安全的,但您可以切换到ConcurrentQueue<char>

如果您需要其他数据结构,.NET 4中还有其他线程安全的集合,请参阅http://msdn.microsoft.com/en-us/library/dd997305.aspx


编辑:在.NET 3.5中,同步原语较少。您可以通过在Queue<char>周围添加一个锁来创建一个简单的解决方案,尽管它的效率低于.NET 4的ConcurrentQueue。或者使用相同的StrignBuffer,再次使用lock读/写操作:

public static StringBuilder strBuilderBuffer = new StringBuilder("", 7500);
private object BufferLock = new object();

...

lock (BufferLock)
    strBuilderBuffer.Append(strSomeData);

...

string strCurrentBuffer;
lock (BufferLock)
{
    strCurrentBuffer = ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.ToString();
    ThreadWorker_TestThreadSafety_v1a.strBuilderBuffer.Clear();
}
Console.WriteLine("[BufferAnalyze()]  ||<<  Thread 'Randomly' Slept ...");
Thread.Sleep(1000);  //  Simulate poor timing of thread resourcing...

编辑:

您不能保证操作系统不会挂起持有锁的工作线程。但是,只要一个线程正在处理它,锁就可以保证其他线程不会干扰并更改缓冲区。

这就是为什么你握住锁的时间应该尽可能短:

  • 采取锁定,添加数据,释放锁定, - 或 -
  • 采取锁定,复制数据,清空缓冲区,释放锁定,开始处理复制的数据。

答案 2 :(得分:1)

如果你从缓冲区中读取大量内容,也许这会有所帮助:

http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlock.aspx

可以使用多个读者,但只有一个作者。

它可以在.NET 1.X及以上版本中使用...