正在加载图片...
2 CHAPTER 1 Hello,world of concurrency in C++! multithreaded C++programs without relying on platform-specific extensions and thus allow writing portable multithreaded code with guaranteed behavior.It also comes at a time when programmers are increasingly looking to concurrency in general,and multi- threaded programming in particular,to improve application performance. This book is about writing programs in C++using multiple threads for concur- rency and the C++language features and library facilities that make that possible.I'll start by explaining what I mean by concurrency and multithreading and why you would want to use concurrency in your applications.After a quick detour into why you might not want to use it in your applications,I'll give an overview of the concur- rency support in C++,and I'll round off this chapter with a simple example of C++ concurrency in action.Readers experienced with developing multithreaded applica- tions may wish to skip the early sections.In subsequent chapters I'll cover more extensive examples and look at the library facilities in more depth.The book will fin- ish with an in-depth reference to all the C++Standard Library facilities for multi- threading and concurrency. So,what do I mean by concurrency and multithreading? 1.1 What is concurrency? At the simplest and most basic level,concurrency is about two or more separate activi- ties happening at the same time.We encounter concurrency as a natural part of life; we can walk and talk at the same time or perform different actions with each hand, and of course we each go about our lives independently of each other-you can watch football while I go swimming,and so on. 1.11 Concurrency in computer systems When we talk about concurrency in terms of computers,we mean a single system per- forming multiple independent activities in parallel,rather than sequentially,or one after the other.It isn't a new phenomenon:multitasking operating systems that allow a single computer to run multiple applications at the same time through task switch- ing have been commonplace for many years,and high-end server machines with mul- tiple processors that enable genuine concurrency have been available for even longer. What is new is the increased prevalence of computers that can genuinely run multiple tasks in parallel rather than just giving the illusion of doing so. Historically,most computers have had one processor,with a single processing unit or core,and this remains true for many desktop machines today.Such a machine can really only perform one task at a time,but it can switch between tasks many times per second.By doing a bit of one task and then a bit of another and so on,it appears that the tasks are happening concurrently.This is called task switching. We still talk about concurrency with such systems;because the task switches are so fast, you can't tell at which point a task may be suspended as the processor switches to another one.The task switching provides an illusion of concurrency to both the user and the applications themselves.Because there is only an illusion of concurrency,the2 CHAPTER 1 Hello, world of concurrency in C++! multithreaded C++ programs without relying on platform-specific extensions and thus allow writing portable multithreaded code with guaranteed behavior. It also comes at a time when programmers are increasingly looking to concurrency in general, and multi￾threaded programming in particular, to improve application performance. This book is about writing programs in C++ using multiple threads for concur￾rency and the C++ language features and library facilities that make that possible. I’ll start by explaining what I mean by concurrency and multithreading and why you would want to use concurrency in your applications. After a quick detour into why you might not want to use it in your applications, I’ll give an overview of the concur￾rency support in C++, and I’ll round off this chapter with a simple example of C++ concurrency in action. Readers experienced with developing multithreaded applica￾tions may wish to skip the early sections. In subsequent chapters I’ll cover more extensive examples and look at the library facilities in more depth. The book will fin￾ish with an in-depth reference to all the C++ Standard Library facilities for multi￾threading and concurrency. So, what do I mean by concurrency and multithreading? 1.1 What is concurrency? At the simplest and most basic level, concurrency is about two or more separate activi￾ties happening at the same time. We encounter concurrency as a natural part of life; we can walk and talk at the same time or perform different actions with each hand, and of course we each go about our lives independently of each other—you can watch football while I go swimming, and so on. 1.1.1 Concurrency in computer systems When we talk about concurrency in terms of computers, we mean a single system per￾forming multiple independent activities in parallel, rather than sequentially, or one after the other. It isn’t a new phenomenon: multitasking operating systems that allow a single computer to run multiple applications at the same time through task switch￾ing have been commonplace for many years, and high-end server machines with mul￾tiple processors that enable genuine concurrency have been available for even longer. What is new is the increased prevalence of computers that can genuinely run multiple tasks in parallel rather than just giving the illusion of doing so. Historically, most computers have had one processor, with a single processing unit or core, and this remains true for many desktop machines today. Such a machine can really only perform one task at a time, but it can switch between tasks many times per second. By doing a bit of one task and then a bit of another and so on, it appears that the tasks are happening concurrently. This is called task switching. We still talk about concurrency with such systems; because the task switches are so fast, you can’t tell at which point a task may be suspended as the processor switches to another one. The task switching provides an illusion of concurrency to both the user and the applications themselves. Because there is only an illusion of concurrency, the
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有