W
W
Wing02016-06-08 16:27:20
C++ / C#
Wing0, 2016-06-08 16:27:20

The program closes when Parallel.For?

Hello, there is a program that calculates the trajectory of an object. Because of the long calculation, I used Parallel.For, but the program simply closed (most often without an error). Who knows what's wrong?

private void выполнитьРасчетToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            Random rand = new Random();
            var ob = new object();
            var before = DateTime.Now;
            List = new List<double[]>();
            Parallel.For(0, Prec, val =>
            {
                double b;
                double c;
                double d = 0;//theta1
                double m = 0;//phi1
                double f = 0;
                double g = 0;//theta2
                double h = 0;//phi2
                double i1 = 0;
                double k = 0;
                int l = 0;
                for (b = rand.Next() % 900 / 10.0, c = rand.Next() % 3600 / 10.0; (Math.Abs(c - 180.0) / 180.0 > Math.Sin(b * Math.PI / 180.0)); b = rand.Next() % 900 / 10.0, c = rand.Next() % 3600 / 10.0) ;
                c = 180.0 + (c - 180.0) / Math.Sin(b * Math.PI / 180.0);
                dllsub(ref A, ref b, ref c, out d, out m, out f, out g, out h, out i1, out k, out l);
                if (g > ThetaO - Aamp && g < ThetaO + Aamp)
                {
                    lock (List)
                    {
                        List.Add(project(d, m, g, h, A, f, h));
                    }
                }
            });
            double mit = MinT(List);
            for (int i = 0; i < List.Count; i++) List[i][2] = Time(List[i][3], List[i][2] - mit, A);
            List.Sort(new QuantComparer());
            this.Refresh();
            foreach (double[] a1 in DrawL) richTextBox1.Text = richTextBox1.Text + (int)Math.Round(a1[0]) + "\t" + (int)Math.Round(a1[1]) + "\t" + Math.Round(a1[2]) + "\n";
        }

Sometimes this error pops up: Attempt to read or write to protected memory. This is often an indication that other memory is corrupted.
In the output window, everything is written incomprehensible, except for this: The program '[7132] WindowsFormsApplication2.vshost.exe: Managed (v4.0.30319)' has exited with code 127 (0x7f).
dllsub - procedure on fortran'e, with the help of which we get the coordinates of the body. We put them on the list.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Lastochkin, 2016-06-08
@Wing0

Most likely trap in unmanaged code, which probably does not support multithreading. You can try to put a global lock on the dllsub() call to make sure.
Code style sucks.

D
Dasha Tsiklauri, 2016-06-08
@dasha_programmist

first: never use a random class from multiple threads, it is non-thread-safe and sometimes (in practice at high frequency of generation) the instance "breaks" and starts always returning 0
second: the dllsub, project method remains a mystery, since they use external variables - this is important

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question