C ++ parallel programming 1
We can watch TV while singing. Parallel processing is easy to understand, but what about computers? Is it because we are editing Word documents and listening to songs that computers are doing in parallel? Not necessarily, if your computer is a single core, it will not be parallel. Instead, it will divide your tasks into task slices that you don't even feel, and it will be almost executed in parallel, it is actually executed in serial mode. If it is a dual-core task, it may be that two tasks are executed simultaneously on one core, or parallel tasks are executed on different cores. There are two ways to implement parallel execution: multi-process: startup is complicated and slow. The operating system requires additional resources to manage processes, but it is easy to write code that is safer than the thread. Multithreading: threads are easier to communicate with each other, and startup and communication are less costly than processes. Why use concurrency: the rational use of a splitting task usually involves three parallel methods: task parallelism Splits a task into different parts, and executes data parallelism in parallel to divide the data into different parts, the thread performs the same operation on different data. The embarrassingly parallel algorithm becomes the parallel execution algorithm why not use concurrency to execute tasks easily. However, thread startup also requires too many startup threads, which can easily consume the memory and address space (especially for 32-bit machines ), it is easy to consume system resources, such as creating a thread for each link, as well as switching overhead. Hello world needs to be compiled in the C ++ 11 environment. # Include <iostream> # include <thread> void hello () {std: cout <"hello world" <std: endl;} int main () {std:: thread t (hello); t. join (); return 0 ;}