Published on

Slide deck from my presentation on multi-threading with .NET. The presentation covers from beginner onwards and looks at current technologies (i.e. pre .NET 4.0) specifically.

What makes this extra special is the entire process of how I prepared for it, from finding content to slide deck layout to presentation prep is documented at:

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Initially do finding primes then speed it up with simple threadingDemo notes – get someone to note the times in milliseconds and maybe run twice to get an average. Also note the amount of primes (should always 9593, within first 100000) being displayed in test 2 may differ between runs – this is important to point out as it leads to the next topicAlso get task monitor open for this set – show the difference between using one and two cores (from test 1 to test 2)All demos should be run in release mode (Ctrl+F5) unless otherwise specifiedOpen up the base threading solution, go over the core code of demo set 1.Paste in line Ctrl+1 within the loopText should be: TestPrime(primeCounter);Run, show results, explain the problem domain and these can be worked out individuallyPaste in line Ctrl+2 – replacing the text within the loop. Text should be: new Thread(new ParameterizedThreadStart(TestPrime)).Start(primeCounter);Should have an exception on the TestPrime because it’s signature does not match.Paste in line Ctrl+3 below the main method, should be ok to run now.Text should be: private static void TestPrime(object objectToTest) { int valueToTest = Convert.ToInt32(objectToTest); TestPrime(valueToTest);}-< END >-
  • Fixing the code by adding synchronisationFirst step add code in Ctrl+4, replacing existing thread start, and explain properties and that maybe (ha ha) if we up the priority it will help – might be good point to wake the audience to see who thinks it will.Text should be: Thread primeThread = new Thread(new ParameterizedThreadStart(TestPrime)); primeThread.Name = primeCounter.ToString(); primeThread.Priority = ThreadPriority.Highest; primeThread.Start(primeCounter);Note that by now we should have a few different numbers of found primesNext add code from Ctrl+5, to the variables at the top – the idea here is to add items to the “gate” and remove them once calculated and do not finish until all the numbers are worked outText should be: private static List<Int32> gate = new List<Int32>();Ctrl+6 into the loop above the code from beforeText should be: gate.Add(primeCounter);Ctrl+7 into the overrided TestPrime method we added before, at the endText should be: gate.Remove(valueToTest);Add the next code, Ctrl+8, after the loop so that we are forced to wait until the gates have been clearedRun in debug mode and wait for the crash – the list is being manipulated in an odd way and the item was gone before we could get it.Right now explain we need to make sure that only one thread at a time can access the list to fix it.Ctrl+9 to the variablesText should be: private static object locker = new object();Replace the gate.add and gate.remove we added eariler with the code from Alt+1 and Alt+2 respectivelyText (Alt+1) should be: lock (locker) { gate.Add(primeCounter); }Text (Alt+2) should be: lock (locker) { gate.Remove(valueToTest); }Run in release and note the time and itemsExplain that the amount of threads is hurting the CPU, increasing deadlocks etc… so lets limit the treads Add Alt+3 after the lock for adding to the gate, but before we spin up a threadText should be: while (gate.Count > 10) { Thread.Sleep(0);}-< END >-
  • This is used for lock-free code - making sure that changes made on one thread are visible to another without incurring the cost of a lock. It does not control thread synchronization, unlike lock
  • Volatile DemoCLR memory model… Essentially the memory model allows for non-volatile readswrites to be reordered as long as that change can not be noticed from the point of view of a single thread. The issue is, of course, there is often more than one thread (like the finalizer thread, worker threads, threadpool threads, etc). volatile essentially prevents that optimization. Replace the two variables we added in the last demo with the new one in Alt+4Text should be: private static int poolSize;Replace the gate.add (lock and all with) the interlocked from Alt+5Text should be: Interlocked.Increment(ref poolSize);Replace the while on the gate with the code from Alt+6Text should be: while (poolSize > 10) { Thread.Sleep(0); }Replace the gate.remove (lock and all) with the interlocked code from Alt+7Text should be: Interlocked.Decrement(ref poolSize);Replace the check at the end of the loop with code from Alt+8Text should be: while (poolSize > 0) { Thread.Sleep(0); }Run and check the speed increase, if any-< END >-
  • Using ThreadPool to find primesNow replace all the code in the main loop, with the exception of the Interlocked.Increment with Alt+9Text should be: ThreadPool.QueueUserWorkItem(TestPrime, primeCounter);RUN-< END >-
  • WinForms DemoStart off explaining what we are trying to do – we have fully working SINGLE threaded solution going on here. First step is to make the status bar keep track of what we are doing.Add backgroundWorker control and add a DoWork event, and in there add the code from Ctrl+Alt+1Text should be: while (true) { toolStripStatusLabel.Text = primesList.Items.Count.ToString(); Thread.Sleep(250); }Add the kick off, Ctrl+Alt+2, to the form constructor below the initialisationText should be: backgroundWorker.RunWorkerAsync();RUNNow remove the background worker component and related code. Add the following code to the class (Ctrl+Alt+3)Text should be: private void timerTick(object state) { toolStripStatusLabel.Text = primesList.Items.Count.ToString(); }And add Ctrl+Alt+4 to the constructorText should be: System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(timerTick), null, 0, 100);RUN-< END >-
  • Add the code from Ctrl+Alt+5 to the loop replacing the code which calls TestprimeText should be: ThreadPool.QueueUserWorkItem(ThreadedTestPrime, primeCounter);Run and crashNow add Ctrl+Alt+6 to the isPrime block – lots of explaining needed now. Remove the add to list line.Text should be: AddItemDelegate addItemMethod = new AddItemDelegate(AddItem); this.Invoke(addItemMethod, valueToTest);Now add Ctrl+Alt+7 outside of any methodText should be: delegate void AddItemDelegate(int value);Run – point out that although we are doing a separate thread it is still not very responsive etc…Add Ctrl+Alt+8 to the new AddItem method we added previouslyText should be: Application.DoEvents();
  • Notification of every DLL in the process is on creation AND destruction
  • A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable. Then the first thread and second thread perform their operations on the value, and they race to see which thread can write the value last to the shared variable. The value of the thread that writes its value last is preserved, because the thread is writing over the value that the previous thread wrote.
  • Show the debugging window for threading (if you it is gone, it is under Debug > Windows > Threading)Output window for debugging messages-< END >-
  • Multi-Threading

    1. 1. Multi-Threading<br />
    2. 2. .NET Multi-Threading<br />Introductory Usage<br />Proven Practices<br />Overture<br />
    3. 3. Hello to the threading world<br />
    4. 4. Blocking<br />Thread.Sleep & Join<br />Locking<br />lock, Mutex & Semaphore<br />Singling<br />EventWaitHandle& Wait/Pulse<br />Non-Blocking<br />Memory Barrier, Interlocked & Volatile<br />Managing<br />
    5. 5. Managing threads<br />
    6. 6. static void Main() <br />{<br />Threadt = newThread (delegate() {<br />Console.ReadLine(); }); <br /> t.Start(); <br /> t.Join(); // Wait until thread finishes<br /> /* Do next step */<br />}<br />Join<br />
    7. 7. lock(locker)<br />{<br /> gate.Add(primeCounter);<br />}<br />Monitor.Enter(locker);<br />try<br />{<br />gate.Add(primeCounter);<br />}<br />finally<br />{<br />Monitor.Exit(locker);<br />}<br />Sugary lock<br />
    8. 8. Same as a lock<br />Advantage: Can work across processes, meaning multiple applications can use a single mutex<br />Mutex<br />
    9. 9. staticEventWaitHandle wh = new AutoResetEvent(false);<br />static void Main() {<br />newThread (Waiter).Start();<br />Thread.Sleep (1000); // Wait for some time...<br />wh.Set(); // OK - wake it up<br />}<br />static void Waiter() {<br />Console.WriteLine("Waiting...");<br />wh.WaitOne(); // Wait for notification<br />Console.WriteLine("Notified");<br />}<br />Signalling<br />
    10. 10. lock(locker)<br />{<br /> gate.Add(primeCounter);<br />}<br />Thread.MemoryBarrier();<br />try<br />{<br />gate.Add(primeCounter);<br />}<br />finally<br />{<br />Thread.MemoryBarrier();<br />}<br />Memory Barrier<br />
    11. 11. static void Main() {<br />Thread t = new Thread(delegate() {<br />try {<br />Thread.Sleep(Timeout.Infinite); // This is blocking<br /> }<br />catch (ThreadInterruptedException) {<br />Console.Write("Forcibly ");<br /> }<br />Console.WriteLine("Woken!");<br /> });<br /> t.Start();<br /> t.Interrupt();<br /> }<br />Interrupt<br />
    12. 12. Similar usage to Interrupt<br />Throws – ThreadAbortException<br />Does not wait for blocking<br />Abort<br />
    13. 13. Using non-blocking management<br />
    14. 14. Using the built in ThreadPools<br />
    15. 15. Single thread pool per process<br />Built in logic to grow and shrink pool<br />Built-in Limits<br />Worker threads – 25 per CPU<br />I/O threads – 1000 per CPU<br />Thread Pools<br />
    16. 16. Threaded controls in WinForms<br />
    17. 17. Timers<br />Source Alex Calvo -<br />
    18. 18. Talking to the UI thread from a worker thread<br />
    19. 19. “Multi-threaded programming needs a little care” <br />Patricia Shanahan<br />Understatement?<br />
    20. 20. Cost<br />Thread Safety<br />Race conditions<br />Dead locks<br />Exception Management<br />Debugging<br />Common Problems<br />
    21. 21. 1 Mb of Address space<br />12k for kernel mode stack<br />Notification of every DLL in the process<br />Threads are expensive<br />
    22. 22. Reads value (5)<br />Adds 1 – (6)<br />Assumes value 6<br />Reads value (6)<br />Adds 1 – (7)<br />Assumes value 7<br />Thread 1<br />Thread 2<br />Race Conditions<br />
    23. 23. If file does not exist<br />Create it<br />Populate it with initial data<br />Read data from file<br />Process data<br />Write to file<br />Logical Process I<br />
    24. 24. If file does not exist<br />Create it<br />Populate it with initial data (Write Lock)<br />Read data from file (Read Lock)<br />Process data<br />Write to file (Write Lock – if needed)<br />Release locks<br />Logical Process II<br />
    25. 25. Creates file<br />Locks writer<br />Attempts to lock reader<br />Waits…<br />Locks Reader<br />Processes data<br />Attempts to lock writer<br />Waits…<br />Thread 1<br />Thread 2<br />Dead Locks<br />
    26. 26. Exceptions are limited to a thread<br />Exceptions<br />Main<br />Child<br />try<br />{<br />}<br />catch<br />{<br />}<br />1<br />
    27. 27. Debugging in Visual Studio<br />
    28. 28. Threads are expensive – use wisely<br />Use managed threads over native threads.<br />Use Timers or ThreadPool where possible.<br />Or the new parallel extensions in .NET 4.0<br />Avoid mutex, unless you need cross process.<br />Avoid Thread.Abortas it can have high side effects<br />Avoid Thread.Suspend/Resume as a blocking system<br />Rather use lock<br />Proven Practises I<br />
    29. 29. Be careful what you lock<br />lock’s are type based shared across all AppDomains. Use static as the solution<br />this (instances) have a high chance of deadlocks<br />Use lock over Monitor<br />If you must use Monitor, use try...finally<br />Inside a lock do as little as possible<br />If you are doing math inside the lock rather change to Interlocked<br />Never perform long running operations on the UI thread<br />Proven Practises II<br />
    30. 30.
    31. 31.<br /> <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />,guid,f8404ab3-e3e6-4933-a5bc-b69348deedba.aspx<br /><br /><br /><br /><br />References<br />