简体   繁体   中英

Using public static object for locking thread shared resources

Lets Consider the example, I have two classes:

Main_Reader -- Read from file

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

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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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