简体   繁体   English

使用公共静态对象锁定线程共享资源

[英]Using public static object for locking thread shared resources

Lets Consider the example, I have two classes: 让我们考虑这个例子,我有两个类:

Main_Reader -- Read from file Main_Reader从文件读取

public  class Main_Reader
{
   public static object tloc=new object();
   public void Readfile(object mydocpath1)
   {
       lock (tloc)
       {
           string mydocpath = (string)mydocpath1;
           StringBuilder sb = new StringBuilder();
           using (StreamReader sr = new StreamReader(mydocpath))
           {
               String line;
               // Read and display lines from the file until the end of 
               // the file is reached.
               while ((line = sr.ReadLine()) != null)
               {
                   sb.AppendLine(line);
               }
           }
           string allines = sb.ToString();
       }
   }
}

MainWriter -- Write the file MainWriter写入文件

public  class MainWriter
{
  public void Writefile(object mydocpath1)
  {
      lock (Main_Reader.tloc)
      {
          string mydocpath = (string)mydocpath1;
          // Compose a string that consists of three lines.
          string lines = "First line.\r\nSecond line.\r\nThird line.";

          // Write the string to a file.
          System.IO.StreamWriter file = new System.IO.StreamWriter(mydocpath);
          file.WriteLine(lines);
          file.Close();
          Thread.Sleep(10000);
          MessageBox.Show("Done----- " + Thread.CurrentThread.ManagedThreadId.ToString());
      }
  }
}

In main have instatiated two function with two threads. 主要具有两个线程实例化的两个功能。

 public string mydocpath = "E:\\testlist.txt";  //Here mydocpath is shared resorces
     MainWriter mwr=new MainWriter();

     Writefile wrt=new Writefile();

    private void button1_Click(object sender, EventArgs e)
    {
        Thread t2 = new Thread(new ParameterizedThreadStart(wrt.Writefile));
        t2.Start(mydocpath);
        Thread t1 = new Thread(new ParameterizedThreadStart(mrw.Readfile));
        t1.Start(mydocpath);
        MessageBox.Show("Read kick off----------");

    }

For making this thread safe, i am using a public static field, 为了使该线程安全,我使用了公共静态字段,

public static object tloc=new object();   //in class Main_Reader

My Question is, is this a good approach? 我的问题是,这是一个好方法吗?

Because I read in one of MSDN forums: 因为我在一个MSDN论坛中阅读:

avoid locking on a public type 避免锁定公共类型

Is there another approach for making this thread safe? 还有另一种方法可以使该线程安全吗?

I believe the MSDN statement has meaning if you share your code with other people. 我相信,如果您与其他人共享代码,则MSDN声明具有意义。 You never know if they are going to use the locks properly, and then your threads might get blocked. 您永远不知道它们是否会正确使用锁,然后您的线程可能会被阻塞。 The solution is probably to write both thread bodies into the same class. 解决方案可能是将两个线程主体都写入同一个类。

On the other hand, since you're dealing with files, the filesystem has a locking mechanism of its own. 另一方面,由于您要处理文件,因此文件系统具有自己的锁定机制。 You won't be allowed to write into a file that is being read, or read of file that is being written. 您将无法写入正在读取的文件或读取正在写入的文件。 In a case like this, I would perform the reading and the writing in the same thread. 在这种情况下,我将在同一线程中执行读取和写入操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM