Concurrent programming of c++11 (I.)

Source: Internet
Author: User

The future of chip manufacturing, if not break the 5nm limit, then in a period of time the performance of the CPU, will rely on three-dimensional integration technology, more CPU cores to integrate together, making multicore systems more and more common.

Formerly known as C + + multithreading, one is limited by the platform, more with the help of packaged APIs to complete, such as: POSIX threads,windows threads, and so on, the second is limited to single-core systems, essentially "pseudo-multithreading", through the scheduling of threads, Enables single-core systems to switch tasks, resulting in a multi-threading illusion.

The new C++11 standard, which implements multi-threading at the language level , provides the relevant components in the library, making it possible to write multi-threaded CPP programs across platforms, and ultimately to achieve true parallel computing in multicore systems.

1 Concurrency (concurrency)

1.1 Concurrency and parallelism

concurrency in a computer refers to the simultaneous execution of multiple independent activities in a single system. For multicore systems, the activities they perform at the same time are called parallelism .

Popular understanding: When two people are eating and watching TV, for a person, eating and watching TV is concurrent, for two people, their activities at the same moment, can be called parallel.

1) Single-core task switching

2) parallel execution of dual cores (parallel execution)

1.2 Threads and processes

If the process is likened to a house , then the people who live in the house, the various activities they engage in (for example, cooking in the kitchen, eating in the living room, watching TV in the bedroom) are threads . Now another person, when two people are in the house, doing their own activities , the program from the previous single thread into multi-threaded .

Some rooms (such as a living room) can be in and out of two people, which means that some of the memory space in the process is shared, and each thread can use the shared memory . Some rooms (such as toilets) can only hold one person at a time, first go in the door lock, after the people see the lock, wait outside, until the advanced person to open the lock, this is the " Mutex " (mutex)

In the application, the specific use of concurrent programming, one is multi-process concurrency, and the second is multithreading concurrency, such as:

(1) Multi-process concurrency (2) Multithreading concurrency

2 program Examples

  implementing multithreading requires 1) header file <thread>   2) separate thread function ThreadFunc ()   3) Thread object thread t (threadfunc)    4) Wait for thread join ()

#include <thread><iostream>void  ThreadFunc () {    "This is a" thread! " << Std::endl;} int Main () {    std::thread T (threadfunc);    T.join ();         " This is main program! " << Std::endl;         return 0 ;}

The output is:

 is a thread!   main program!

When T.detach () is used instead of t.join () , the main thread main does not wait for the new thread T (threadfunc). will only run to the end of the program alone.

3 Tasks instead of threads

3.1 Two questions

1) thread exhaustion (exhaustion)

A software thread is a limited resource, and when the number of threads created is more than the system can provide , an exception std::system_error is thrown and the program terminates.

2) thread overage (oversubscription)

When there are more threads waiting to run (Ready-to-run) than the system hardware thread (hardware threads), the thread scheduler allocates time slices (time-slice) on the hardware thread for each software thread. If the time slice of one thread ends, the context switch is executed when the time slice of the other thread is just beginning. For multicore systems, when a thread is switched from one CPU to another , it can cause significant resource consumption.

3.2 Task-based (task-based)

Based on thread programming (thread-based), it is necessary to manually manage the above two problems, which increases the difficulty of programming.

Task-based programming (task-based), which simplifies the process by std::async the problem to the C + + standard library.

1) header file <future>

2) separate task functions taskFunc1 and taskFunc2

3) Asynchronous Task Object Auto FUT1 = Std::async (taskFunc1)

4) Get Task function return value fut.get ()

#include <iostream>#include<thread>#include<future>std::stringtaskFunc1 () {std::stringstr ="This is Task1"; returnstr;} STD::stringTaskFunc2 () {std::stringstr ="and Task2"; returnstr;}intMain () {Auto FUT1=Std::async (TASKFUNC1); Auto Fut2=Std::async (TASKFUNC2); Std::cout<< FUT1.Get() + Fut2.Get() << Std::endl <<"This is the main program"<<Std::endl; return 0;}

The output is:

is was main program 

Summary:

1) thread-based Programming needs manual management of thread exhaustion, oversubscription, load balancing, and a Daptation to new platforms.

2) task-based Programming handles most of these issues via Std::async with the default launch policy

Resources:

<c++ Concurrency in action> Chapter 1

<effecctive Modern C++> Chapter 7

Concurrent programming of c++11 (I.)

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.