←to practical programming

Multiprocessing

Symmteric multiprocessing and threads

Symmetric multiprocessing (SMP) referes to computer hardware where two or more identical processors are connected to a single shared main memory and are controlled by a single instance of an operating system. Most ordinary computers today use the SMP architecture.

When a program uses two or more processors on an SMP computer—to share the workload and speedup execution—it is broadly refered to as multiprocessing or parallel computing. A part of a program that can run independently on a single processor is refered to—in this context—as a thread. Multiprocessing is generally achieved when the master thread of a program—the one where the Main function runs—forks off a number of extra threads which execute blocks of code in parallel on the available processors.

Threads in C-sharp

In C-sharp the System.Threading namespace contains classes to work with threads. In particular, the System.Threading.Thread class can create a separate thread that executes a given function (a delegate in C-sharp language). The operating system will then be able to run this thread on a separate processor.

The delegate to be run in a separate thread can be of two types: a void function without arguments, or a void function with one argument of type object,

public delegate void procedure_without_arguments();
public delegate void procedure_with_one_argument(object? obj);
// "?" signifies the object can be null
The procedure can cast the argument to whatever type it needs.

Suppose one wants to calculate a harmonic sum, Σi=a…b 1/i, in a separate thread. One could do it like this,

public class data { public int a,b; public double sum; } // data to pass to/from harmonic_sum

public static void harmonic_sum(object obj){ // the function to run in a thread
	data x = (data)obj; // cast of argument to our data type
	x.sum = 0; for(int i = x.a; i <= x.b; i++) x.sum += 1.0/i;
	}

public static void Main(){
	data x = new data();
	x.a = 1; x.b = 1000; /* prepare data */
	Thread t = new Thread(harmonic_sum); /* prepare a thread */
	t.Start(x); /* run the t-thread on a (hopefully) separate processor */
	/* meanwhile do some work in the main thread */
	t.Join(); /* join the t-thread with the main thread */
	WriteLine($"harmonic sum from {x.a} to {x.b} equals {x.sum}");
}

Dotnet parallel programming

In addition to the Thread class dotnet implements Parallel.For and Parallel.Foreach loops (and several other parallel constracts) which might automatically distribute the computational load over several processors.