天天看点

Chapter 1 Hello, world of concurrency in C++!

What is concurrency?

1. At the simplest and most basic level, concurrency is about two or more separate activities happening at the same time.

2. When we talk about concurrency in terms of computers, we mean a single system performing  multiple  independent  activities  in  parallel,  rather  than  sequentially,  or  one after the other. 

3. 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 .

Chapter 1 Hello, world of concurrency in C++!

4. Whether they have multiple processors  or  multiple  cores  within  a  processor  (or  both),  these  computers  are  capable  of genuinely running more than one task in parallel. We call this  hardware concurrency.

5. The first approach is to have multiple single-threaded processes, which is similar to having each developer in their own office, and the second approach is to have multiple threads in a single process, which is like having two developers in the same office.

6. The first way to make use of concurrency within an application is to divide the application into multiple, separate,

single-threaded  processes  that  are  run  at  the  same  time, much as you can run your web browser and word processor  at  the  same  time.

Chapter 1 Hello, world of concurrency in C++!

7. The  alternative  approach  to  concurrency  is  to  run  multiple  threads  in  a  single  process. Threads are much like lightweight processes: each thread runs independently of the  others,  and  each  thread  may  run  a  different  sequence  of  instructions.  But  all threads  in  a  process  share  the  same  address  space,  and  most  of  the  data  can  be accessed directly from all threads—global variables remain global, and pointers or references to objects or data can be passed around among threads. 

Chapter 1 Hello, world of concurrency in C++!

8. But  the  flexibility  of  shared  memory  also  comes  with  a  price:  if data is accessed by multiple threads, the application programmer must ensure that the view of data seen by each thread is consistent whenever  it  is  accessed. 

9. The low overhead associated with launching and communicating between multiple threads within a process compared to launching and communicating  between  multiple  single-threaded  processes  means  that  this  is  the  favored approach to concurrency in mainstream languages including C++, despite the potential problems arising from the shared memory. 

Why use concurrency?

1. There  are  two  main  reasons  to  use  concurrency  in  an  application:  separation  of  concerns and performance.

2. Separation of concerns is almost always a good idea when writing software; by grouping  related  bits  of  code  together  and  keeping  unrelated  bits  of  code  apart,  you  can make  your  programs  easier  to  understand  and  test,  and  thus  less  likely  to  contain bugs.

3. There are two ways to use concurrency for performance. The first, and most obvious,  is  to  divide  a  single  task  into  parts  and  run  each  in  parallel,  thus  reducing  the total runtime. This is  task parallelism. The second way to use concurrency for performance is to use the available parallelism to solve bigger problems; rather than processing one file at a time, process 2 or 10 or 20, as appropriate. 

Concurrency and multithreading in C++

1. All this changes with the release of the new C++11 Standard. Not only is there a brand-new thread-aware memory model, but the C++ Standard Library has been extended to include  classes  for  managing  threads, protecting  shared  data, synchronizing operations between threads, and low-level atomic operations.

Getting started

1.traditional C++ code for outputing "hello world" in a single thread.

#include <iostream>
int main()
{
    std::cout<<"Hello World\n";
}
           

2.Concurrent program for writing "Hello world" to the console.

#include <iostream>
#include <thread>           
void hello()                       
{
    std::cout<<"Hello Concurrent World\n";
}
int main()
{
    std::thread t(hello);   
    t.join();                      
}
           

Summary

In this chapter, I covered what is meant by concurrency and multi-threading and why you’d choose to use it (or not) in your applications. I also covered the history of multi-threading  in  C++  from  the  complete  lack  of  support  in the  1998  standard,  through various platform-specific extensions, to proper multi-threading support in the new C++

Standard,  C++11.  This  support  is  coming  just  in  time  to  allow  programmers  to  take advantage of the greater hardware concurrency becoming available with newer  CPUs, as chip manufacturers choose to add more processing power in the form of multiple cores  that  allow  more  tasks  to  be  executed  concurrently,  rather  than increasing  the execution speed of a single core.

     I also showed how simple using the classes and functions from the C++ Standard Library  can  be,  in  the examples  in  section  1.4.  In  C++,  using  multiple  threads  isn’t complicated  in  and  of  itself;  the  complexity lies in  designing  the  code  so  that  it behaves as intended.

继续阅读