Multithreading .NET (II)

By | 21 August, 2013

Of course using multithreading have some drawbacks. For instance the slower execution time when running multithreaded code in a single-core machine caused by the context-switching overhead at the time that the scheduler lets each existing thread run for a little assigned time and then it suspends it to let next thread run for a little bit of time again.

Another issue to take into account is the extra code you’ll need in terms of accomplishing the same task (chap, writing code to multithreads is, sometimes, hard work). And of course, our dear maintainability will be surely (seriously) affected. Not to mention the testability, how we could deal with the idea of the same inputs may produce different outputs, how the scheduler decide which process and when is going to share the CPU time, or just recreate the bug that a potential customer report… Full mesh!

Ehhhh, don’t be discouraged! Taking these insignificant aspects into account, let’s take a look to the coarse-grained.

Some things to know about starting threads. These are explicitly started using System.Threading.Thread.
Constructor is used to set thread entry point, and properties such as name or priority, can be set post-construction, afterwards you’ll notice the useful ManagedThreadId that allow us to know the unique ID that the CLR assigns to this thread. In the same way, we can notice the number of processors or core of the machine in which is being executed the app, just using the property of the Environment: ProcessorCount.

Thread execution begins when Start method is called.


using System;
using System.Threading;

class StartingThreads
{
    //Entry point of the app
    static void Main()
    {

        Console.WriteLine("-- #{0}# Startin Main method --", Thread.CurrentThread.ManagedThreadId);


        Console.WriteLine("-- #{0}# Number of processors = {1} --",
                          Thread.CurrentThread.ManagedThreadId,
                          Environment.ProcessorCount);

        Thread t = new Thread(InitCardinalPoint);

        // Properties for the thread
        t.Priority = ThreadPriority.BelowNormal;
        t.Name = "Hello evil cardinal point";

        //Begins the thread execution
        t.Start();
        t.Join();

        Console.WriteLine("-- #{0}# Finishing Main method --", Thread.CurrentThread.ManagedThreadId);
    }

    static void InitCardinalPoint ()
    {
        //This number will be replaced by the unique ID that the CLR assigns to this thread 
        Console.WriteLine("-- #{0}# Evil cardinal point of the 4Vientos Compass! --", Thread.CurrentThread.ManagedThreadId);
    }
}

So the final resulting display:

The Main method was called by the one with Id number 9 and then displayed the processor count which on this machine is 4 (ok, this is not funny, but you can also prove this in a single-core machine). But the thread 10 was the one which called the function down here. So this proves how successful we are at starting a completely separate thread of execution.

Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Leave a Reply

Your email address will not be published.