C ++ concurrent programming learning notes & lt; 1 & gt; getting started, programming learning notes

Source: Internet
Author: User

C ++ concurrent programming learning notes <1> getting started, programming learning notes
Getting started

 

 

 

What does a multi-threaded C ++ program look like?

 

 

It looks like all other C ++ programs, usually a combination of variables, classes, and functions. The only real difference is that some functions can run concurrently,

Of course, to run functions concurrently, you must use specific functions and objects to manage various threads.

 

 

 

 

Starting from hello world

 

 

Let's start with a classic example: a program that prints "Hello World. A very simple Hello, World running in a single thread

The program is as follows. When we talk about multithreading, it can be used as a benchmark,

 

#include<iostream>int main(){    std::cout<<"hello world" ;    return 0;}

 

What this program does is to write "Hello World" into the standard output stream. Let's compare it with the simple Hello,

The Concurrent World program starts an independent thread to display this information.

 

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


 

<1> The first difference is that # include <thread> is added. The declarations supported by multithreading in the Standard C ++ library are in this header file.

 

<2> second, the code for printing information is moved to an independent function. This is because each thread must have an initial function and the execution of the new thread

The line starts here. For applications, the initial thread is main (), but for all other threads, the thread object t has the new function hello.

() As its initial function.

 

<3> unlike writing standard output directly or calling hello () from main (), this program starts a new thread for implementation. The initial thread starts with main ()

The new thread starts with hello ().

 

<4> after the new thread t (hello) is started, the initial thread (main) continues to run. If it does not wait until the end of the new thread t (hello ),

Continue to run until the end of main (), and end the program -- it is possible that the new thread t (hello) will end the program before it has finished running. That's why

The reason why join () is called here in callback, which causes the calling thread (main) to wait for t (hello) to finish executing and then end the initial thread (main ).

 

 

 

 

Summary:

 

In general, it is not worth using multithreading for such a simple task, especially if the initial thread is idle during this period. However, in some scenarios

Multithreading can achieve clear benefits.

 

 

 

 

 


Which of the following is more suitable for beginners?

If you do not want to study this major, you will have a better one-stop experience. This book also introduces a lot of computer knowledge (I don't know how many computers have graduated .. ).

Do you have any C Language Study Notes or prepared some documents?

Hundred examples of C language programming: it is easier to learn from examples. It is better than simply reading those books with principles //
Is it a procedural test or a theoretical test?
Recommended program design guidance and online practices for trial use, outline-level books. Link: ai.pku.edu.cn/book/
The theoretical written examination must be dominated by Tan haoqiang, but it is indeed a bit messy. You may wish to buy a related exercise or something like this. Tan haoqiang has many related exercises in this book, which are basically the same, choose based on the existing materials on hand. We will not recommend it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.