Unformatted text preview:

6.826—Principles of Computer Systems 2006 Handout 16. Paper: Programming with Threads 1 16. Paper: Programming with Threads The attached paper by Andrew Birrell, Introduction to Programming with Threads, originally appeared as report 35 of the Systems Research Center, Digital Equipment Corp., Jan. 1989. A somewhat revised version appears as chapter 4 of Systems Programming with Modula-3, Greg Nelson ed., Prentice-Hall, 1991, pp 88-118. The current version has been revised to use the C# language (similar to Java) and incorporate some new material. Read it as an adjunct to the lecture on practical concurrency. It explains how to program with threads, mutexes, and condition variables, and it contains a lot of good advice and examples.ȱAn Introduction to Programming with C# Threads Andrew D. Birrell [Revised May, 2005] ȱThisȱ paperȱ providesȱ anȱ introductionȱ toȱ writingȱ concurrentȱ programsȱ withȱ “threads”.ȱ Aȱthreadsȱ facilityȱ allowsȱ youȱ toȱ writeȱ programsȱ withȱ multipleȱ simultaneousȱ pointsȱ ofȱexecution,ȱ synchronizingȱ throughȱ sharedȱ memory.ȱ Theȱ paperȱ describesȱ theȱ basicȱ threadȱandȱsynchronizationȱprimitives,ȱthenȱforȱeachȱprimitiveȱprovidesȱaȱtutorialȱonȱhowȱtoȱuseȱit.ȱ Theȱ tutorialȱ sectionsȱ provideȱ adviceȱ onȱ theȱ bestȱ waysȱ toȱ useȱ theȱ primitives,ȱ giveȱwarningsȱaboutȱwhatȱcanȱgoȱwrongȱandȱofferȱhintsȱaboutȱhowȱtoȱavoidȱtheseȱpitfalls.ȱTheȱpaperȱ isȱ aimedȱ atȱ experiencedȱ programmersȱ whoȱ wantȱ toȱ acquireȱ practicalȱ expertiseȱ inȱwritingȱ concurrentȱ programs.ȱ Theȱ programmingȱ languageȱ usedȱ isȱC#,ȱ butȱ mostȱ ofȱ theȱtutorialȱappliesȱequallyȱwellȱtoȱotherȱlanguagesȱwithȱthreadȱsupport,ȱsuchȱasȱJava.ȱCategoriesȱ andȱ Subjectȱ Descriptors:ȱ D.1.3ȱ [Programmingȱ Techniques]:ȱ ConcurrentȱProgramming;ȱ D.3.3ȱ [Programmingȱ Languages]:ȱ Languageȱ Constructsȱ andȱ Features—Concurrentȱprogrammingȱstructures;ȱD.4.1ȱ[OperatingȱSystems]:ȱProcessȱManagementȱGeneralȱTerms:ȱDesign,ȱLanguages,ȱPerformanceȱAdditionalȱ Keyȱ Wordsȱ andȱ Phrases:ȱ Threads,ȱ Concurrency,ȱ MultiȬprocessing,ȱSynchronizationȱȱCONTENTS 1. Introduction .............................................................................................................. 12. Whyȱuseȱconcurrency? ............................................................................................ 23. Theȱdesignȱofȱaȱthreadȱfacility ................................................................................ 34. UsingȱLocks:ȱaccessingȱsharedȱdata....................................................................... 85. UsingȱWaitȱandȱPulse:ȱschedulingȱsharedȱresources......................................... 176. UsingȱThreads:ȱworkingȱinȱparallel..................................................................... 267. UsingȱInterrupt:ȱdivertingȱtheȱflowȱofȱcontrol ................................................... 318. Additionalȱtechniques ........................................................................................... 339. AdvancedȱC#ȱFeatures........................................................................................... 3610. Buildingȱyourȱprogram ......................................................................................... 3711. Concludingȱremarks .............................................................................................. 38ȱ©ȱMicrosoftȱCorporationȱ2003,ȱ2005.ȱPermissionȱtoȱ copyȱinȱ wholeȱorȱ partȱ withoutȱpaymentȱofȱ feeȱisȱ grantedȱforȱ nonȬprofitȱeducationalȱandȱresearchȱpurposesȱprovidedȱthatȱallȱsuchȱwholeȱorȱpartialȱcopiesȱ includeȱ theȱ following:ȱ aȱ noticeȱ thatȱ suchȱ copyingȱ isȱ byȱ permissionȱ ofȱMicrosoftȱCorporation;ȱanȱacknowledgementȱofȱtheȱauthorȱofȱtheȱwork;ȱandȱthisȱcopyrightȱnotice.ȱPartsȱofȱthisȱworkȱareȱbasedȱonȱresearchȱreportȱ#35ȱpublishedȱinȱ1989ȱ byȱ theȱ Systemsȱ Researchȱ Centerȱ ofȱ Digitalȱ Equipmentȱ Corporationȱ andȱcopyrightȱ byȱ them.ȱ Thatȱ materialȱ isȱ usedȱ hereȱ byȱ kindȱ permissionȱ ofȱ HewlettȬPackardȱCompany.ȱAllȱrightsȱreserved.ȱȱȱȱAn Introduction to Programming with C# Threads . 1 1. INTRODUCTION Almostȱeveryȱmodernȱ operatingȱ systemȱorȱprogramming ȱ environmentȱprovidesȱsupportȱ forȱ concurrentȱ programming.ȱ Theȱ mostȱ popularȱ mechanismȱ forȱ thisȱ isȱsomeȱ provisionȱ forȱ allowingȱ multipleȱ lightweightȱ “threads”ȱ withinȱ aȱ singleȱaddressȱspace,ȱusedȱfromȱwithinȱaȱsingleȱprogram.ȱProgrammingȱ withȱthreadsȱintroducesȱ newȱdifficultiesȱevenȱ forȱexperiencedȱprogrammers.ȱ Concurrentȱprogrammingȱhasȱtechniquesȱ andȱ pitfallsȱ thatȱ doȱ notȱoccurȱinȱsequentialȱprogramming.ȱManyȱofȱtheȱtechniquesȱareȱobvious,ȱbutȱsomeȱareȱ obviousȱ onlyȱ withȱ hindsight.ȱ Someȱ ofȱ theȱ pitfallsȱ areȱ comfortableȱ (forȱexample,ȱ deadlockȱ isȱ aȱ pleasantȱ sortȱ ofȱ bug—yourȱ programȱ stopsȱ withȱ allȱ theȱevidenceȱintact),ȱbutȱsomeȱtakeȱtheȱformȱofȱinsidiousȱperformanceȱpenalties.ȱTheȱpurposeȱofȱthisȱpaperȱisȱtoȱgive ȱyouȱanȱintroductionȱtoȱtheȱprogrammingȱtechniquesȱ thatȱ workȱ wellȱ withȱ threads,ȱ andȱ toȱ warnȱ youȱ aboutȱ techniquesȱ orȱinteractionsȱ thatȱ workȱ outȱ badly.ȱ Itȱ shouldȱ provideȱ theȱ experiencedȱ sequentialȱprogrammerȱwithȱ enoughȱhintsȱ toȱbeȱ ableȱ toȱbuildȱ aȱ substantialȱ multiȬthreadedȱprogramȱthatȱworks—correctly,ȱefficiently,ȱandȱwithȱaȱminimumȱofȱsurprises.ȱThisȱ paperȱ isȱ aȱ revisionȱ ofȱ oneȱ thatȱ Iȱ originallyȱ publishedȱ inȱ 1989ȱ [2].ȱ Overȱtheȱ yearsȱ thatȱ paperȱ hasȱ beenȱ usedȱ extensivelyȱ inȱ teachingȱ studentsȱ howȱ toȱprogramȱwithȱthreads.ȱButȱaȱlotȱhasȱchangedȱinȱ14ȱyears,ȱbothȱinȱlanguageȱdesignȱandȱ inȱ computerȱ hardwareȱ design.ȱ Iȱ hopeȱ thisȱ revision,ȱ whileȱ presentingȱessentiallyȱ theȱ sameȱ ideasȱ asȱ theȱ earlierȱ paper,ȱ willȱ makeȱ themȱ moreȱ accessibleȱandȱmoreȱusefulȱtoȱaȱcontemporaryȱaudience.ȱAȱ“thread”ȱ isȱaȱ straightforwardȱ concept:ȱaȱsingleȱsequentialȱ flowȱ ofȱ


View Full Document

MIT 6 826 - Programming with Threads

Documents in this Course
Consensus

Consensus

10 pages

Load more
Download Programming with Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming with Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming with Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?