0

According to: https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx, "By default, a StreamWriter is not thread safe. See TextWriter.Synchronized for a thread-safe wrapper." So I've written a method and used TextWriter.Synchronized to synchronize my StreamWriter object:

        static void GenerateData()
    {
        string fileName = Path.Combine(directory, "filenames.txt");

        using(StreamWriter names = new StreamWriter(fileName))
        {
            // Synchronize access to names
            TextWriter.Synchronized(names);

            // Create 100,000 test files
            // Take advantage of parallelism
            Parallel.For(0, 100001, i =>
                {
                    // Generate name of test file
                    string curFile = Path.Combine(directory, String.Format("test{0}.txt", i + 1));

                    // Store test file name in filenames.txt
                    names.WriteLine(curFile);

                    using(StreamWriter writer = new StreamWriter(curFile))
                    {
                        // Take advantage of multiple cores
                        //TextWriter.Synchronized(writer);

                        // For 30 lines ..
                        for(int j = 0; j < 30; j++)
                        {
                            List<Rule> rules = new List<Rule>();

                            // .. generate i rules
                            for(int k = 0; k < i; k++)
                            {
                                // Name of rule starts with r followed by random number in range [0, 9999]
                                string name = "r" + String.Format("{0}", random.Next(0, 10000));
                                // Rule priority is in range [0, 500]
                                int pty = random.Next(1, 501);
                                rules.Add(new Rule(name, pty));
                            }

                            // Write the list of rules to curFile
                            writer.WriteLine(String.Join(", ", rules.ToArray()));
                        };
                    }
                });
        }
    }

However despite this, some lines in the output file (filenames.txt) still get jumbled up with others. I would like to know if this can be fixed without resorting to locks on the StreamWriter.

Sample output:

  1. C:\Users\Frank\Documents\visual studio 2013\Projects\TimedAssignment3\TimedAssignment3\Datasets\test1.txt
  2. C:\Users\Frank\Documents\visual studio 2013\Projects\TimedAssignment3\TimedAssignment3\Datasets\test50001.txt
  3. C:\Users\Frank\Documents\visual studio 2013\Projects\TimedAssignment3\TimedAssignment3\Datasets\tesal studio 2013\Projects\TimedAssignment3\TimedAssignment3\Datasets\test37501.txt
  4. C:\Users\Frank\Documents\visual studio 2013\Projects\TimedAssignment3\TimedAssignment3\Datasets\test87501.txt
  5. C:\Users\Frank\Documents\visual studio 2013\Projects\TimedAssignment3\TimedAssignment3\Datasets\test12501.txt
2
  • What do you mean by "jumbled up"? Can you show us the contents of filenames.txt after it has been written so we know what you mean? Commented Apr 5, 2015 at 5:17
  • @ConstantCoder I've added several lines of the output Commented Apr 5, 2015 at 6:00

1 Answer 1

3

Use returned TextWriter like this:

var syncWriter = TextWriter.Synchronized(names);

MSDN(https://msdn.microsoft.com/en-us/library/system.io.textwriter.synchronized(v=vs.110).aspx) says:

All write operations to the returned wrapper will be thread safe. You call this method to ensure that only one thread at a time can execute the methods on the TextWriter instance that is returned.

Synchronized() doesn't make the instance 'synchronized' but returns new synchronized instance.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.