Multithreading in C# - part 1

Here I posted multithreading in C++11. Well I did ran into issues with C++ library. To validate I tried the same dotproduct example in C#.

Here is the code for the same:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class DotProduct
    {
        double[] a;
        double[] b;
        double[] res;
        int id;
        int numElems;

        public DotProduct(double[] aa, double[] bb,
            double[] r, int nElems, int ident)
        {
            a = aa;
            b = bb;
            res = r;
            id = ident;
            numElems = nElems;
        }

        public void Evaluate()
        {
            res[id] = 0;
            for (int idx = id * numElems, end = (id + 1) * numElems;
                idx < end; ++idx)
            {
                res[id] += a[idx] * b[idx];
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int numElements = 100000;
            double[] a = new double[numElements];
            double[] b = new double[numElements];

            for (int idx = 0; idx < numElements; ++idx)
            {
                a[idx] = idx;
                b[idx] = numElements - idx;
            }

            double[] result = new double[4];
            int numElems = numElements / 4;

            DotProduct dp1 = new DotProduct(a, b, result, numElems, 0);
            System.Threading.Thread thread1 =
                new System.Threading.Thread(new System.Threading.ThreadStart(dp1.Evaluate));

            DotProduct dp2 = new DotProduct(a, b, result, numElems, 1);
            System.Threading.Thread thread2 =
                new System.Threading.Thread(new System.Threading.ThreadStart(dp2.Evaluate));

            DotProduct dp3 = new DotProduct(a, b, result, numElems, 2);
            System.Threading.Thread thread3 =
                new System.Threading.Thread(new System.Threading.ThreadStart(dp3.Evaluate));

            DotProduct dp4 = new DotProduct(a, b, result, numElems, 3);
            System.Threading.Thread thread4 =
                new System.Threading.Thread(new System.Threading.ThreadStart(dp4.Evaluate));

            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();

            thread1.Join();
            thread2.Join();
            thread3.Join();
            thread4.Join();

            System.Console.WriteLine("Dotproduct is {0} ",
                (result[0] + result[1] + result[2] + result[3]));

            System.Console.ReadKey();

        }
    }
}

First we create DotProduct class that holds the arrays and result array. Every DotProduct class has an id to identify the range of array it is going to operate on. Next we create 4 threads and pass-in for instances of DotProduct and the method to be executed on the thread. Unlike C++11, creating thread does not start the thread, it has to be called explicitly. Then as discussed in C++11 multithreading post we wait until all the threads have completed the execution. That’s it. Quite simple. More to come. Stay tuned…