Sunday, 15 September 2013

Multithreading vs Parallel programming


What is Parallelism? Parallelism is taking a certain task and dividing it into a set of related tasks to be executed concurrently. Sweet! So again, thinking of these tasks as threads, Parallel Programming still has the same problems of concurrent programming (deadlocks, data races, etc...), and introduces new challenges, most notably, sharing and partitioning data across these related threads. This makes Parallel Programming even more difficult and debugging even more complicated :
(
However, it is not all bad news. .NET 4.0 parallel programming is a great step onwards. The new API solves a lot of problems (but not all) of Parallel Programming, and greatly eases up parallel debugging...

The Parallel Programming model of .NET 4.0 is composed of the following:
  • The Task Parallel Library (TPL): this is the base of the Task-based programming discussed in the previous section. It consists of:
    • Task class: the unit of work you will code against instead of the previous thread model.
    • Parallel class: a static class that exposes a task-based version of some parallel-nature problems. More specifically, it contains the following methods:
      • For
      • Foreach
      • Invoke
  • Parallel LINQ (PLINQ): built on top of the TPL and exposes the familiar LINQ as Parallel extensions.


Parallel programming is the general discipline of doing multiple computations in parallel, e.g. using multiple cores, each of which is doing some subcomputation of a larger single problem.

Multithreading is the approach of using multiple threads of execution to process different operations, e.g. if you have two things to do, use one thread to do one and another thread to do the other.

An operating system is able to take each of these threads and other multiplex them onto the same core or to run them in parallel on multiple cores that may exist.

As such, multithreading is a common approach to parallel programming, whereby you can split a larger problem into multiple subproblems and use multiple threads (i.e. multithreading) to process those subproblems concurrently.

Multi-threaded programming is the way which allows developers to take advantage of multiple threads concurrently. And while it has been available for many years, multi-threaded programming has often been avoided due to its complexity. Developers had to put most of their effort in writing clean multi-threaded code rather than focusing on the business problem.

What does multithreading have to do with multi-cores? Can't we write multi-threaded code if we have a single core? Yes, we can. But again, as long as we have a single core, only one thread at a time will work since all threads belong to that same single core... logical enough. When your multi-threaded program runs on a multi-core machine, threads belonging to different cores will be able to run together and hence your program will take advantage of the hardware power.

One final note before going on: multithreaded programming does not differ if you have a multi-core machine or multi-processer machine. This is a hardware separation; underneath, your code will be working with threads all the same.


Multi-threaded example:
public class Test{
    static void Main()
    {
        Counter foo = new Counter();
        ThreadStart job = new ThreadStart(foo.Count);
        Thread thread = new Thread(job);
        thread.Start();

        for (int i=0; i < 5; i++)
        {
            Console.WriteLine ("Main thread: {0}", i);
            Thread.Sleep(1000);
        }
    }}public class Counter{
    public void Count()
    {
        for (int i=0; i < 10; i++)
        {
            Console.WriteLine ("Other thread: {0}", i);
            Thread.Sleep(500);
        }
    }}
Example of parallel:
int n = ...Parallel.For(0, n, i =>{
   // ... });

1 comment: