+ All Categories
Home > Documents > Why Be Concurrent?

Why Be Concurrent?

Date post: 22-Feb-2016
Category:
Upload: dewitt
View: 28 times
Download: 0 times
Share this document with a friend
Description:
Why Be Concurrent?. CET306 Harry R. Erwin University of Sunderland. Roadmap. Why Concurrency? Test Driven Development Refactoring Threading in C#. Texts. - PowerPoint PPT Presentation
Popular Tags:
31
Why Be Concurrent? CET306 Harry R. Erwin University of Sunderland
Transcript
Page 1: Why Be Concurrent?

Why Be Concurrent?

CET306Harry R. Erwin

University of Sunderland

Page 2: Why Be Concurrent?

Roadmap

• Why Concurrency?• Test Driven Development• Refactoring• Threading in C#

Page 3: Why Be Concurrent?

Texts• Clay Breshears (2009) The Art of Concurrency: A Thread

Monkey's Guide to Writing Parallel Applications, O'Reilly Media, Pages: 304. This is available on www.amazon.co.uk as a paperback and more cheaply as a Kindle edition. You don't need a Kindle to read a Kindle edition.

• Mordechai Ben-Ari (2006) Principles of Concurrent and Distributed Programming, Addison-Wesley. Also available on www.amazon.co.uk This is available as a paperback only. Covers the theory in depth and will be used as a supplementary text.

Page 4: Why Be Concurrent?

Want to Go Faster?

• I assume you want to write faster code.– You need to be prepared to write the code.– You need to know the syntax.– You need to know the libraries and methods.– You need to be prepared to think carefully and

logically.– You need intuition.

Page 5: Why Be Concurrent?

Definitions

• A system is concurrent if it can time-share.• A system is parallel if it can do more than one

thing simultaneously. This is a subset of concurrent.

Page 6: Why Be Concurrent?

Why Concurrency?

• Multi-core processors are now the state of the art—multiple CPUs running on a shared memory with shared resources.

• The old pattern of Moore’s Law no longer applies 8(. To run faster, you have to share the work among multiple processors.

Page 7: Why Be Concurrent?

A Threading Methodology1. Analysis—similar to specification, where you identify the

code that can run concurrently. As a rule, start with working and already tuned code. Debugging concurrent code is a nightmare. You will use algorithm analysis and a profiler.

2. Design and implementation—do it. The book is mostly about this step.

3. Test for correctness. If the design is provably correct, you avoid much pain here.

4. Tune for performance. Use a profiler and check how well the code is running. Your threading may actually be affecting your tuning.

Page 8: Why Be Concurrent?

Testing and Tuning

• If you modify your code to improve its performance, you can introduce standard bugs and threading bugs.

• Standard bugs are the bugs you can often detect using test-driven design. It’s useful to have a collection of unit tests around to check for them.

• Threading bugs are the really nasty ones, because they depend on thread ordering. Design defensively to avoid these bugs. If you can prove your design is correct, you don’t have to do exhaustive (and exhausting) thread testing.

Page 9: Why Be Concurrent?

Test-Driven Development and Refactoring

• Test-Driven Development• Unit Testing• A Catalogue of Refactorings

Page 10: Why Be Concurrent?

Sources• Fowler, 2000, Refactoring: Improving the Design of Existing

Code, Addison-Wesley. Available electronically at the library.• http://www.amazon.com/exec/obidos/tg/detail/-/03211092

95/103-4060125-0311065

• http://www.amazon.com/exec/obidos/ASIN/0130648841/103-4060125-0311065

• http://www.refactoring.com/catalog/ • http://www.win.ua.ac.be/~lore/refactoringProject/index.ph

p

• Extensive discussions on the comp.object newsgroup.

Page 11: Why Be Concurrent?

Test-Driven Development

• Refactoring depends on having good unit tests. This means you need to be doing test-driven development.

• Test-driven development is a method of software development where tests specify interfaces of implementation and all code must have passed the tests. (Wikipedia)

Page 12: Why Be Concurrent?

How Bob Martin Describes TDD (personal communication)

• Erwin: TDD as I understand it:• 1. Write a test for a bit of functionality.• 2. Show that it fails.• 3. Write the code to make the test pass.• Martin: A good summary, but there's more. • 1. We do not write production code until there is a failing test.• 2. We write the simplest possible production code to get the

test to pass.• 3. We do not write more tests when we have a failing test.• 4. We do not add to a failing test.

Page 13: Why Be Concurrent?

Martin Comments Further• If you watched someone doing TDD you would see them oscillating

between test code and production code once every minute or so. • During each oscillation the programmer would add a few lines to

his test code, thus making it fail (or not compile) and then add just a few lines to his production code in order to make the test pass (or compile).

• Each oscillation is so simple that it's not worth taking. • Each oscillation is so simple that the risk of error is close to zero. • If you walk into a room of people working this way, and chose

anyone at random, a minute ago all his code would have been working.

Page 14: Why Be Concurrent?

Automated Unit Testing

• Originally defined for Java (Junit).• Ported to .Net as NUnit • See http://www.nunit.org/• Also see http://en.wikipedia.org/wiki/

List_of_unit_testing_frameworks • I strongly recommend use of such a

framework.

Page 15: Why Be Concurrent?

Refactoring

• Deals with code rot. Your program works but it has become ugly as it has been modified.

• “Refactoring is a technique to restructure code in a disciplined way. For a long time it was a piece of programmer lore, done with varying degrees of discipline by experienced developers, but not passed on in a coherent way.” (Fowler)

Page 16: Why Be Concurrent?

Refactoring Principles

• Change only the implementations• Use it to improve the design of software• Use it to make software more understandable• Use it to help find bugs• Use it to help you program faster• Use it to prepare your current program for

modification.

Page 17: Why Be Concurrent?

When?

• “Three strikes and you refactor.”– The third time you have to cope with something

ugly, fix it.• particularly…– When you add functionality– When you need to fix a bug– When you do a code review

Page 18: Why Be Concurrent?

The Basic Rule of Refactoring

• “Refactor the low hanging fruit” http://c2.com/cgi/wiki?RefactorLowHangingFruit

• Low Hanging Fruit (def): “The thing that gets you most value for the least investment.”

• In other words, don’t spend much time on it. There are always ways to improve any design incrementally. We will explore a few of them.

Page 19: Why Be Concurrent?

The Goal of Refactoring

• To improve code without changing what it does.

• This in some ways is similar to how an optimizing compiler restructures code.

Page 20: Why Be Concurrent?

Kinds of Refactorings

• Composing Methods• Moving Features Between Objects• Organizing Data• Simplifying Conditionals• Making Method Calls Simpler• Generalization• Big Refactorings

Page 21: Why Be Concurrent?

Recommendations

• Do some reading on Test Driven Development.• Choose and learn to use a unit testing

framework.• Do more reading on refactoring in C#. The

Fowler book is for Java, but the same techniques carry over to C#

• Learn the features supporting refactoring in Visual Studio 2010.

Page 22: Why Be Concurrent?

Refactoring Conclusions

• Kent Beck says: This is “only the beginning.”• Why? Questions we’ve left unaddressed include

when to use refactoring and when to let well enough alone.

• Beck’s advice:– Pick an achievable goal– Stop when you’re unsure– Backtrack if necessary– Work with a partner

Page 23: Why Be Concurrent?

Background of Parallel Algorithms

• Next Week

Page 24: Why Be Concurrent?

Threading in C#

• Creating and running threads• Managing threads• Synchronisation

Page 25: Why Be Concurrent?

General

• To work with threads in C#, you need to import System and System.Threading. The code is:using System;using System.Threading;

• Some key classes are Thread and ThreadStart.

Page 26: Why Be Concurrent?

Creating and Running Threads

• In C# you use a ThreadStart delegate to create a Thread. The argument to ThreadStart is the name of a method that returns void and will be the run() method of the thread.

• The Thread constructor can implicitly wrap its argument in a ThreadStart delegate.

• Thread can also take a lambda expression as an argument.– Thread t = new Thread( ()=>aMethod(arguments));

Page 27: Why Be Concurrent?

Example PseudoCodeNamespace whatever{

class Foo {// various variables and constructorspublic void Dowhatever() {etc.} // the run methodstatic void Main(String[] args){

Foo f = new Foo(whatever);Thread t = new Thread(new

ThreadStart(f.Dowhatever);t.start();

}}

}

Page 28: Why Be Concurrent?

Managing Threads

• This is where the various Thread methods come in. (next slide)

• The ThreadPool class is available and is used to make efficient use of multiple threads. You queue work items in the ThreadPool and provide callback delegates to be executed when the work items complete.

• We’ll get into this in more detail later.

Page 29: Why Be Concurrent?

Thread

• Interesting members include:– Thread.Sleep(msec); —note this is class level—– Name(string); —give it a name—– Priority(ThreadPriority); —give it a priority—– Abort(); —die—– Interrupt(); —wake immediately—– Join(); —have the calling thread wait until exit—– Resume(); —unsuspend—– Start(); —start the thread—– Suspend(); —take a break—

Page 30: Why Be Concurrent?

Synchronisation• If threads share data, access must be managed to avoid

conflicts.• One class used to manage this is the Monitor class. If you

want to lock an Object, call Monitor.Enter(Object). When you’re done with it, remember to call Monitor.Exit(Object). This needs to be packaged in a try{} finally{} construct to avoid errors making locks permanent.

• The easiest way to do this is lock(this){etc.}

which hides the hassle from you.

Page 31: Why Be Concurrent?

Conclusion

• The tutorial today plays around with these ideas. See you there!


Recommended