Synchronize Threads

3

I have an application with 3 threads, this application involves a chemical process simulator, and I have to pick up some values from these processes. the values come in a single object.

The 3 threads make an infinite loop, in this loop they take the values at different times, which can not happen.

I wanted to know if I could sync threads, so when they get the object all 3 pick up at the same time.

Here are my threads:

public void RunController()
{
     Softing.OPCToolbox.Client.Application app = Softing.OpcToolbox.Client.Application.Instance;
     app.Initialize();
     while(true)
     {
          scase.Solver.Integrator.IsRunning = true;
          Thread.Sleep(1000);
          scase.Solver.Integrator.IsRunning = false;
          scase = Interaction.GetObject(opcform.pasta);
          Contrl();
          GC.Collect();
     }
}




public void RunExport()
{
     Softing.OPCToolbox.Client.Application app = Softing.OpcToolbox.Client.Application.Instance;
     app.Initialize();
     while(true)
     {
          scase.Solver.Integrator.IsRunning = true;
          Thread.Sleep(1000);
          scase.Solver.Integrator.IsRunning = false;
          scase = Interaction.GetObject(opcform.pasta);
          Exprt();
          GC.Collect();
     }
}



public void RunImport()
{
     Softing.OPCToolbox.Client.Application app = Softing.OpcToolbox.Client.Application.Instance;
     app.Initialize();
     while(true)
     {
          scase.Solver.Integrator.IsRunning = true;
          Thread.Sleep(1000);
          scase.Solver.Integrator.IsRunning = false;
          scase = Interaction.GetObject(opcform.pasta);
          Imprt();
          GC.Collect();
     }
}

I would like them to run scase = Interaction.GetObject(opcform.pasta); at the same time.

    
asked by anonymous 17.12.2014 / 13:52

1 answer

2

Given the problem you encounter, I do not find it interesting to create Threads in this way. You would be dealing with unnecessary competition.

You have entries, jobs that need to be executed (maybe in parallel, maybe not), and wait for the outputs. This is a typical example of the use of Task s in C #, which may or may not be executed in parallel.

This article is very interesting about how tasks can be used: link

See this example:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
        Task<Double>[] taskArray = { Task<Double>.Factory.StartNew(() => DoComputation(1.0)),
                                     Task<Double>.Factory.StartNew(() => DoComputation(100.0)), 
                                     Task<Double>.Factory.StartNew(() => DoComputation(1000.0)) };

        var results = new Double[taskArray.Length];
        Double sum = 0;

        for (int i = 0; i < taskArray.Length; i++) {
            results[i] = taskArray[i].Result;
            Console.Write("{0:N1} {1}", results[i], 
                              i == taskArray.Length - 1 ? "= " : "+ ");
            sum += results[i];
        }
        Console.WriteLine("{0:N1}", sum);
   }

   private static Double DoComputation(Double start)
   {
      Double sum = 0;
      for (var value = start; value <= start + 10; value += .1)
         sum += value;

      return sum; 
   }
}

This example starts the calculation in 3 different tasks. When adding the value, using the result task.Result , if the task did not finish, it waits until it finishes executing it without stopping the others. Note that synchronization is not required. You pass the values, they produce a result.

You can literally do a for loop and do when iterations you want. At each iteration, it re-launches the 3 tasks in parallel and rejoins the result.

    
17.12.2014 / 15:39