Danh mục tài liệu

An Introduction to Programming with C#

Số trang: 41      Loại file: pdf      Dung lượng: 762.77 KB      Lượt xem: 21      Lượt tải: 0    
Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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 andsynchronizationprimitives,thenforeachprimitiveprovidesatutorialonhowtouse it.
Nội dung trích xuất từ tài liệu:
An Introduction to Programming with C# An Introduction to Programming with C# ThreadsAndrew D. Birrell[Revised May, 2005]This paper provides an introduction to writing concurrent programs with “threads”. Athreads facility allows you to write programs with multiple simultaneous points ofexecution, synchronizing through shared memory. The paper describes the basic threadandsynchronizationprimitives,thenforeachprimitiveprovidesatutorialonhowtouseit. The tutorial sections provide advice on the best ways to use the primitives, givewarningsaboutwhatcangowrongandofferhintsabouthowtoavoidthesepitfalls.Thepaper is aimed at experienced programmers who want to acquire practical expertise inwriting concurrent programs. The programming language used is C#, but most of thetutorialappliesequallywelltootherlanguageswiththreadsupport,suchasJava.Categories and Subject Descriptors: D.1.3 [Programming Techniques]: ConcurrentProgramming; D.3.3 [Programming Languages]: Language Constructs and Features—Concurrentprogrammingstructures;D.4.1[OperatingSystems]:ProcessManagementGeneralTerms:Design,Languages,PerformanceAdditional Key Words and Phrases: Threads, Concurrency, Multi‐processing,SynchronizationCONTENTS1. Introduction .............................................................................................................. 12. Whyuseconcurrency? ............................................................................................ 23. Thedesignofathreadfacility ................................................................................ 34. UsingLocks:accessingshareddata....................................................................... 85. UsingWaitandPulse:schedulingsharedresources......................................... 176. UsingThreads:workinginparallel ..................................................................... 267. UsingInterrupt:divertingtheflowofcontrol ................................................... 318. Additionaltechniques ........................................................................................... 339. AdvancedC#Features........................................................................................... 3610. Buildingyourprogram ......................................................................................... 3711. Concludingremarks .............................................................................................. 38©MicrosoftCorporation2003,2005.Permissiontocopyinwholeorpartwithoutpaymentoffeeisgrantedfornon‐profiteducationalandresearchpurposesprovidedthatallsuchwholeorpartialcopies include the following: a notice that such copying is by permission ofMicrosoftCorporation;anacknowledgementoftheauthorofthework;andthiscopyrightnotice.Partsofthisworkarebasedonresearchreport#35publishedin1989 by the Systems Research Center of Digital Equipment Corporation andcopyright by them. That material is used here by kind permission of Hewlett‐PackardCompany.Allrightsreserved. An Introduction to Programming with C# Threads . 11. INTRODUCTIONAlmosteverymodernoperatingsystemorprogrammingenvironmentprovidessupport for concurrent programming. The most popular mechanism for this issome provision for allowing multiple lightweight “threads” within a singleaddressspace,usedfromwithinasingleprogram. Programmingwiththreadsintroducesnewdifficultiesevenforexperiencedprogrammers.Concurrentprogramminghastechniquesandpitfallsthatdonotoccurinsequentialprogramming.Manyofthetechniquesareobvious,butsomeare obvious only with hindsight. Some of the pitfalls are comfortable (forexample, deadlock is a pleasant sort of bug—your program stops with all theevidenceintact),butsometaketheformofinsidiousperformancepenalties. Thepurposeofthispaperistogiveyouanintroductiontotheprogrammingtechniques that work well with threads, and to warn you about techniques orinteractions that work out badly. It should provide the experienced sequentialprogrammerwithenoughhintstobeabletobuildasubstantialmulti‐threadedprogramthatworks—correctly,efficiently,andwithaminimumofsurprises. This paper is a revision of one that I originally published in 1989 [2]. Overthe years that paper has been used extensively in teaching students how toprogramwiththreads.Butalothaschangedin14years,bothinlanguagedesignand in computer hardware design. I hope this revision, while presentingessentially the same ideas as the earlier paper, will make them more accessibleandmoreusefultoacontemporaryaudience. A“thread”isastraightforwardconcept:asinglesequentialflowofcontrol.Inahigh‐levellanguageyounormallyprogramathreadusingprocedurecallsormethod calls, where the calls follow the traditional stack discipline. Within asinglethread,thereisatanyinstantasinglepointofexecution.Theprogrammerneedlearnnothingnewtouseasinglethread. Having “multiple threads” in a program means that at any instant theprogram has multiple points of execution, one in each of its threads. Theprogrammercanmostlyviewthethreadsasexecutingsimultaneously,asifthecomputer were endowed with as many processors as there are threads. Theprogrammerisrequiredtodeci ...