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 .

                           

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.

                                           

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. 

                                                     

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.

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值