![]() CATEGORIES: BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism |
Chapter 28 I/O-Bound Asynchronous Operations 727How Windows Performs I/O Operations.................................................. 727 C#’s Asynchronous Functions.................................................................... 732 How the Compiler Transforms an Async Function into a State Machine............................................................................... 734 Async Function Extensibility....................................................................... 738 Async Functions and Event Handlers........................................................ 741 Async Functions in the Framework Class Library.................................... 742 Async Functions and Exception Handling............................................... 744 Other Async Function Features.................................................................. 745 Applications and Their Threading Models............................................... 748 Implementing a Server Asynchronously.................................................. 751 Canceling I/O Operations........................................................................... 751 Some I/O Operations Must Be Done Synchronously.............................. 752 FileStream-Specific Issues............................................................ 753 I/O Request Priorities................................................................................... 754
Chapter 29 Primitive Thread Synchronization Constructs 757 Class Libraries and Thread Safety............................................................. 759 Primitive User-Mode and Kernel-Mode Constructs................................ 760 User-Mode Constructs.................................................................................. 762 Volatile Constructs.............................................................................. 762 Interlocked Constructs....................................................................... 768 Implementing a Simple Spin Lock.................................................. 773 The Interlocked Anything Pattern..................................................... 776 Kernel-Mode Constructs.............................................................................. 778 Event Constructs................................................................................. 782 Semaphore Constructs...................................................................... 784 Mutex Constructs................................................................................ 785 Chapter 30 Hybrid Thread Synchronization Constructs 789 A Simple Hybrid Lock................................................................................... 790 Spinning, Thread Ownership, and Recursion.......................................... 791 Hybrid Constructs in the Framework Class Library................................. 793 The ManualResetEventSlim and SemaphoreSlim Classes... 794 The Monitor Class and Sync Blocks............................................. 794 The ReaderWriterLockSlim Class.............................................. 800 The OneManyLock Class................................................................... 802 The CountdownEvent Class............................................................ 804 The Barrier Class............................................................................ 805 Thread Synchronization Construct Summary............................... 805 The Famous Double-Check Locking Technique..................................... 807 The Condition Variable Pattern.................................................................. 811 Asynchronous Synchronization.................................................................. 814 The Concurrent Collection Classes........................................................... 818 Index 823
Xx Contents Foreword
When you sign up for marriage, you really are living Groundhog Day. If you haven’t seen that movie, watch it, because you will suddenly see why you have to make the same mistakes over and over again. In this case, when Jeff said he wouldn’t write another book, I knew it was the empty promise of an addict. Jeff cannot not write another book. Just today, we were discussing another book he is absolutely not going to write (except that there is already a chapter in progress). It is coded in his DNA. A thoroughbred is born to run and Jeff is born to write. Jeff is as predictable as the seasons. He cannot stay away from the little 0s and 1s locked inside his hard drive. They cannot be ignored. And while the rest of you are all snug in your beds, Jeff’s internal alarm starts ringing around 3:00 a.m. (coincidently, when our four-year-old climbs into bed with us, another pattern I seem to have no control over) some mysterious force compels Jeff’s brain to unlock little solutions, big brainstorms, and frightening bugs that control him. It forces him into his office to work them out of his head. The rest of us can roll over and go back to sleep, safe, knowing that Jeff is out there somewhere solving these problems for us—like a cyber-super hero, saving the thread from becoming just another loose end. But accruing this knowledge just for himself is not enough for Jeff. He feels selfish hoarding his insights in his little space in the universe. So he must broadcast them; he must write them down. They're like radio waves hurling outward wondering if a listener will pick them up. This he does for you, dear reader; a testament to his passion for Microsoft technologies. This book is actually adding a new layer of wisdom. Jeff is getting older each time he flies around the sun, and with the accumulation of years, he is starting to look back. Thinking about things in a more mature manner, he has rewritten the chapter cover- ing Reflection. Maybe you too will join him as he waxes poetic on this subject. This is a place where we can learn how to have the code ask about the code and really encour- aging some deeper insights as to why Reflection works the way it does. Put on your smoking jackets, sink into a leather chair, and spend some time thinking about your own code and its greater purpose in life. On a more lively note, there is stuff about async/await in here. Apparently, this is the progression of the AsyncEnumerator my love has been going on about for some time. Whew, I didn’t think we would ever move on from that! The thing is, as many times as he has talked about his AsyncEnumerator, it hasn’t stuck in my mind at all. I decided if
I knew what enumerator meant, maybe this would help me. According to Wikipedia, it is a census taker. Is this chapter about census takers working out of synchronicity? That seems like a waste of taxpayer dollars. Whatever it means in the computer world must be better than that. Jeff worked with the team at The Big M to perfect async/await, and now it is here in this book laid out for your reading pleasure; I suggest you read it sequentially. Another major addition to the book is the one I’m most excited about. It is my expectation of you all to read and internalize this WinRT stuff. This is a nerd word that somehow means: “Make Me Really Cool Apps for Some Awesome Slate Device NOW!” That’s right; the new Windows Runtime is all about awesome touch screens. My kids would like some birds that fly into pigs. I would like maybe something with flowers, and definitely you could use it for some educational stuff. Just let your imagination go! Come up with Wonderful Innovative Nifty Really Touchy stuff. Use this chapter for my benefit, please. Otherwise, I may run out of patience with Jeff and his continuous book writing and lock him in a room with knitting needles and no electricity. You program- mers decide: write cool apps with WinRT or no new books from Jeff! In summary, with your continued patronage, Jeff has delivered yet another mas- terpiece. Our family can return to a more normal state. Which is what, really? I think maybe normal is the book writing state. Ever patiently awaiting the siren call of yet another book, Kristin Trace (Jeff’s wife) October 2012
Help! Please save Jeff from the knitting!
Introduction
.NET Framework, the common language runtime (CLR), and the C# programming language to me. The moment I saw all of this, I was impressed and I knew that it was going to change the way I wrote software in a very significant way. I was asked to do some consulting for the team and immediately agreed. At first, I thought that the .NET Framework was an abstraction layer over the Win32 API and COM. As I invested more and more of my time into it, however, I realized that it was much bigger. In a way, it is its own operating system. It has its own memory manager, its own security system, its own file loader, its own error handling mechanism, its own application isolation bound- aries (AppDomains), its own threading models, and more. This book explains all these topics (and more) so that you can effectively design and implement software applica- tions and components for this platform. It is October 2012 as I write this text, making it 13 years now that I’ve worked with the .NET Framework and C#. Over the 13 years, I have built all kinds of applications and, as a consultant to Microsoft, have contributed quite a bit to the .NET Framework itself. As a partner in my own company, Wintellect (http://Wintellect.com), I have worked with numerous customers to help them design software, debug software, performance-tune software, and solve issues they have with the .NET Framework. All these experiences have really helped me learn the spots that people have trouble with when trying to be productive with the .NET Framework. I have tried to sprinkle knowledge from these experiences through all the topics presented in this book.
Date: 2016-03-03; view: 819
|