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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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…