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 ...
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 ...
Tìm kiếm theo từ khóa liên quan:
ngôn ngữ lập trình chuyên ngành lập trình ngôn ngữ lập trình C giáo trình ngôn ngữ C tổng quan về ngôn ngữ CTài liệu có liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 317 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 310 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 293 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 251 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 248 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 242 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 232 1 0 -
101 trang 211 1 0
-
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 204 0 0 -
Thiết kế mạch logic bằng Verilog - HDL
45 trang 197 0 0