4

I am reading the text file consisting of 6 columns. Among 6 columns each 3 columns show one object information I want to access these columns in parallel through multithreading. Like 3 columns for one object, altogether 2 threads have created except main thread. The text file looks like this:Text file Image
I tried it but I face difficulty in passing data from the main thread to other threads error occurs at string variable "part". (variable part doesn't exist in the current context)

I want to do multithreading for tag1 and tag2.

I am sharing the block of my code, please suggest me where I am mistaken As I am new to multithread programming.

namespace MultiTag_Simulation_ConsoleApp
{
    class Program
    {    
      static void Main(string[] args)
       {
          string line;
          string[] part;

        StreamReader File = new StreamReader("2Tags_Points.txt");

        while((line = File.ReadLine()) !=null)
        {
            part = line.Split('\t');
            Thread TAG1 = new Thread(new ThreadStart(Tag1));
            TAG1.Start();
        }
    }

    void Tag1()
    {
        double w, x;
        w = Convert.ToDouble(part[1]);
        x = Convert.ToDouble(part[2]);

        Console.WriteLine("Tag1 x:" + w + "\t" + "Tag1 y:" + x);
        Console.ReadKey();
    }
  }
}
SN25
  • 69
  • 1
  • 12
  • 4
    _"please suggest me where I am mistaken As I am new to multithread programming"_ I think you firstly are mistaken in that multithreading is helping here. Mind that creating threads means you'll have an overhead that may be worse than the calculation they do itself. Secondly you are mistaken that you create two threads. In fact you create 2 threads _per line_. – Fildor Dec 13 '17 at 08:03
  • while Tag1 and Tag2 working on part array, main thread changes content of the part array. You need to send the parts to each thread as seperate parameter. Please [chek](https://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread) – Mutlu Kaya Dec 13 '17 at 08:04
  • yes, I want to share the "part" values to these two threads, but I couldn't because I have created these two threads outside the main thread. Please suggest me the way how to use the "part" variable values in these two threads – SN25 Dec 13 '17 at 08:10
  • See [ParametierizedThreadStart](https://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart(v=vs.110).aspx). But as I said - you'll be probably better off without multithreading in this case. – Fildor Dec 13 '17 at 08:15
  • BTW: You have some code duplication there. I'd use one method with a signature of `void PrintTag( string tagName, string tagX, string tagY )` – Fildor Dec 13 '17 at 08:21
  • 2
    Creating and starting threads is extemely expensive, which is way for example for Tasks threads are recycled. Also proper thread Management is difficult. A much better way would be create work packets, i.e. `File.ReadLines(filename).SelectMany(line => { var parts = line.Split('\t'); return new[] { new { Tag = parts[0], X = parts[1], Y = parts[2] }, new { Tag = parts[3], X = parts[4], Y = parts[5] }}; })` and use this as input for `Parallel.Foreach`, which will do all the thread management and load-balancing for you. – ckuri Dec 13 '17 at 08:30
  • I don't understand your point? I am sorry – SN25 Dec 13 '17 at 08:31
  • To explain more, in this code `File.ReadLines` will go through all lines lazily (i.e. only read the lines when they are required) and split each line into two parts: Tag1 and Tag2 denoted by an anonymous class with the properties Tag, X and Y. Let's say this new enumerable is called `packets`. You can than call `Parallel.ForEach(packets, packet => PrintTag(packet))` (see Fildor's comment) on this, which will process each packet/tag in parallel. – ckuri Dec 13 '17 at 08:39

2 Answers2

1

Thank you, everyone, for your time. I had mistaken in thread synchronization. Now I solved the issue, by initializing the "part" variable as static variable above the main thread.

 static string [] part 
SN25
  • 69
  • 1
  • 12
0

Your solution, although it might compile, still has a lot of hidden problems. You need to synchronize access to shared variables for example and right now, if you did that, it would defeat the purpose of having multiple threads. I would suggest using a simpler framework, that will do the multi-threading for you, because multi-threading is hard to get right, but using multiple processors for your workload is way easier when you leave the hard stuff to the framework.

For example, this will calculate your stuff in parallel. Although just in parallel per line, not per tag, but as long as all your processors are used optimally, it really does not matter.

namespace MultiTag_Simulation_ConsoleApp
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;

    internal static class Program
    {
        internal static void Main()
        {
            Parallel.ForEach(
                File.ReadLines("2Tags_Points.txt").Select(line => line.Split('\t')),
                parts =>
                    {
                        var w = Convert.ToDouble(parts[1]);
                        var x = Convert.ToDouble(parts[2]);
                        Console.WriteLine("Tag1 x:" + w + "\t" + "Tag1 y:" + x);

                        var y = Convert.ToDouble(parts[4]);
                        var z = Convert.ToDouble(parts[5]);
                        Console.WriteLine("Tag2 x:" + y + "\t" + "Tag2 y:" + z);
                    });
        }
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thank you @nvoigt for your time and reply... Actually, I have to do it for 100 Tags in future... that's why I want to do multithreading. Is multithreading a better approach in this case? – – SN25 Dec 14 '17 at 07:47
  • 1
    @SN25 I think you should program it in a normal loop and with correct results first. Because correct beats fast. If you think the result is not achieved fast enough, *only then* look for ways to optimize it. "A hundred" of something does not sound like the processor would even use it's first core to the fullest, but that's up to you to measure and then decide. – nvoigt Dec 14 '17 at 07:50