Friday 31 March 2017

Threading 07 Barrier

A Barrier is a synchronization primitive that enforces the stopping of execution between a number of threads or processes at a given point and prevents further execution until all threads or processors have reached the given point.

you can think of a barrier as a gate that all of your threads have to reach before moving forward. for example take a look at the following multi thread application.

using System;
using System.Threading;

namespace pc.threadBarrier
{
    class Program
    {
        static Random rnd = new Random(DateTime.Now.Millisecond);
        static void Main(string[] args)
        {
            var pawel = new Thread(CompleteTask);
            pawel.Name = "Pawel";

            var magda = new Thread(CompleteTask);
            magda.Name = "Magda";

            var tomek = new Thread(CompleteTask);
            tomek.Name = "Tomek";

            var jakub = new Thread(CompleteTask);
            jakub.Name = "jakub";

            var marin = new Thread(CompleteTask);
            marin.Name = "Marin";

            var threads = new Thread[] { pawel, magda, tomek, jakub, marin };

            for (int i = 0; i < threads.Length; i++)
                threads[i].Start(((char)(65+i)).ToString());
        }

        static void CompleteTask(object o)
        {
            var sleep = rnd.Next(1000, 2000);
            var s1 = $"{Thread.CurrentThread.Name} is sleeping for {sleep * .001}'s";
            Console.WriteLine(s1);
            Thread.Sleep(sleep);

            Thread.Sleep((int)Char.Parse(o.ToString()));
            var s2 = $"{Thread.CurrentThread.Name} has completed {o.ToString()}";
            Console.WriteLine(s2);
        }
    }
}


we start by creating five threads and passing them a task A to E, each letter has a higher ascii value 65 to 69, this is why after our threads sleep for a random number each one sleeps to the equivalent value of their task letter to ensure that they're printed in the ascending order. however because first each task sleeps for a random time between 1 and 2 seconds this is not possible, what we need is to sync all of our threads after their first sleep.

To sync our threads we use the barrier class.

using System;
using System.Threading;

namespace pc.threadBarrier
{
    class Program
    {
        static Random rnd = new Random(DateTime.Now.Millisecond);
        static Barrier barrier = new Barrier(participantCount:5);
        static void Main(string[] args)
        {
            var pawel = new Thread(CompleteTask);
            pawel.Name = "Pawel";

            var magda = new Thread(CompleteTask);
            magda.Name = "Magda";

            var tomek = new Thread(CompleteTask);
            tomek.Name = "Tomek";

            var jakub = new Thread(CompleteTask);
            jakub.Name = "jakub";

            var marin = new Thread(CompleteTask);
            marin.Name = "Marin";

            var threads = new Thread[] { pawel, magda, tomek, jakub, marin };

            for (int i = 0; i < threads.Length; i++)
                threads[i].Start(((char)(65+i)).ToString());
        }

        static void CompleteTask(object o)
        {
            var sleep = rnd.Next(1000, 2000);
            var s1 = $"{Thread.CurrentThread.Name} is sleeping for {sleep * .001}'s";
            Console.WriteLine(s1);
            Thread.Sleep(sleep);

            //pause thread until participant count hits 5
            barrier.SignalAndWait();
            Thread.Sleep((int)Char.Parse(o.ToString()) * 10);
            var s2 = $"{Thread.CurrentThread.Name} has completed {o.ToString()}";
            Console.WriteLine(s2);
        }
    }
}


after declaring a barrier that all of our threads share we signalandwait, wich will pause all the treads at that point until there are an equal number of signals by the threads as there are participants defined in the constructor of the barrier.

Wednesday 29 March 2017

Threading 06 Signalling via EventWaitHandle

signalling via the EventWaitHandle method involves signalling between threads that one can continue, while the other waits. it facilitates threads waiting for each other at different intervals and signalling to each other that anyone waiting can start.

let's take a look at this

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

namespace pc.SignallingEventWaitHandleWithNumbers
{
    class Program
    {
        static Random rnd = new Random(DateTime.Now.Millisecond);
        static void Main(string[] args)
        {
            var tOdd = new Thread(Print);
            var tEven = new Thread(Print);

            tOdd.Start(Enumerable.Range(0, 20).Where(i => i % 2 == 1));
            tEven.Start(Enumerable.Range(0, 20).Where(i => i % 2 == 0));
        }

        static void Print(object o) {
            var nums = o as IEnumerable<int>;
            foreach (var n in nums)
            {
                Thread.Sleep(rnd.Next(200, 700));
                Console.Write($"{n} ");
            }
        }
    }
}


here we create two threads that print number onto the console at staggering intervals, one thread has even numbers one thread has odd numbers. Each thread prints its own number sequentially but because of the varying delays between prints the numbers come off as staggered.

by leveraging the EventWaitHandler we have the ability to send signals between threads

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

namespace pc.SignallingEventWaitHandleWithNumbers
{
    class Program
    {
        static Random rnd = new Random(DateTime.Now.Millisecond);
        struct Data
        {
            public IEnumerable<int> Nums { get; private set; }
            public EventWaitHandle SignalHandler { get; private set; }

            public Data(IEnumerable<int> Nums, EventWaitHandle SignalHandler)
            {
                this.Nums = Nums;
                this.SignalHandler = SignalHandler;
            }
        }
        static void Main(string[] args)
        {
            var tOdd = new Thread(Print);
            var tEven = new Thread(Print);

            //by setting initialstate to true, the first signal handler wait
            //does not need to be signaled, it recieves a signal by default
            using (var sh = new EventWaitHandle(true, EventResetMode.AutoReset))
            {
                tEven.Start(new Data(Enumerable.Range(0, 20).Where(i => i % 2 == 0), sh));

                //enuser that tEven starts first
                Thread.Sleep(100);
                tOdd.Start(new Data(Enumerable.Range(0, 20).Where(i => i % 2 == 1), sh));

                //wait for both threads to join method not to exit using before
                //finished with SignalHandler
                tEven.Join();
                tOdd.Join();
            }
        }

        static void Print(object o)
        {
            var data = (Data)o;
            foreach (var n in data.Nums)
            {
                //wait for signal
                data.SignalHandler.WaitOne();
                Thread.Sleep(rnd.Next(200, 700));
                Console.Write($"{n} ");
                //send signal
                data.SignalHandler.Set();
            }
        }
    }
}


As One thread waits for a signal the other prints its number and signals that it's complete and waits for a return signal.

Monday 27 March 2017

Visaul studio Shortcuts

Visual Studio 2015 CSS shortcuts

Collapse all:         ctrl m + a
Collapse current:  ctrl m + s

Expand All:          ctrl m + x
Expand Current:   ctrl m + e

Formatting
Peeks at class or function:    ALT+ F12
Format code:                        CTRL + e + d
Comment code :                   CTRL + (E, C) || CTRL + (K, C)
uncomment code :                CTRL + (e, u) || CTRL + (k, u)
Surround code:                     CTRL + (k, s)
Make lowercase:                   CTRL + u
Make uppercase:                   CTRL + SHIFT + u
Fullscreen/exit fullscreen :     ALT +SHIFT + ENTER

Window 
ErrorList :                            Ctrl + W + E
Solution Explorer :              Ctrl + W + S || Alt + Ctrl + L
Server Explore :                  Ctrl + W + L
Command Window :           Ctrl + W + A
Resource Window :             Ctrl + W + R
Tasks Window :                   Ctrl + W + T
Toolbox Window :               Ctrl + W + X

Saturday 25 March 2017

Threading 05 Signalling via CountdownEvent

signalling is way that that threads can communicate or signal each other, there are two types of signalling that can be implemented
  • CountDownEvent which we'll cover in this post
  • EventWaitHandle which we'll look at in the next post
the CountDownEvent signaling mechanism basically creates a barrier (I use this term loosely) that expects a certain number of signals before it will continue, first lets look at the implementation without the use of signalling

using System;
using System.Threading;

namespace pc.ThreadSignalling
{
    class Program
    {
        struct Data {
            public int Delay { get; set; }
            public string Value { get; set; }
            public Data(int Delay, string Value) {
                this.Delay = Delay;
                this.Value = Value;
            }
        }
        static void Main(string[] args)
        {
            var threads = new Thread[6];
            var data = "Hello world my name is Pawel".Split(' ');

            for (int i = 0, delay = 500; i < threads.Length; i++, delay += 500)
            {
                threads[i] = new Thread(Print);
                threads[i].Start(new Data(delay, data[i]));
            }
           
            //wait for each thread to finish before continuing with the main
            foreach (var t in threads)
                t.Join();

            Console.WriteLine("Woot Woot");
        }
        static void Print(object o) {
            var data = (Data)o;
            Thread.Sleep(data.Delay);
            Console.WriteLine(data.Value + " ");
        }
    }
}


Above we join all of our threads to the main before continuing to print out "Woot Woot" now we can accomplish the same thing using countdownevent

using System;
using System.Threading;

namespace pc.ThreadSignalling
{
    class Program
    {
        public struct Data
        {
            public int Delay { get; set; }
            public string Value { get; set; }
            public CountdownEvent cdEvent { get; set; }
            public Data(int Delay, string Value, CountdownEvent cdEvent)
            {
                this.Delay = Delay;
                this.Value = Value;
                this.cdEvent = cdEvent;
            }
        }
        static void Main(string[] args)
        {
            using (var cdEvent = new CountdownEvent(6))
            {
                var threads = new Thread[6];
                var data = "Hello world my name is Pawel".Split(' ');

                for (int i = 0, delay = 500; i < threads.Length; i++, delay += 500)
                {
                    threads[i] = new Thread(Print);
                    threads[i].Start(new Data(delay, data[i], cdEvent));
                }

                //Wait for 6 signals before continuing
                cdEvent.Wait();
            }

            Console.WriteLine("WOot woot");
        }

        static void Print(object o)
        {
            var data = (Data)o;
            Thread.Sleep(data.Delay);
            Console.Write(data.Value + " ");
            data.cdEvent.Signal();
        }
    }
}


In the above instead of joining all the threads in the main, we have each thread signal that it's in a state that can pass the cdEvent.Wait() barrier.