c ++ 11 concurrency and multithreading (Lecture)


    A: basic concepts of concurrency, process


    Concurrent, multithreaded process requirements must master;
    (1.1) concurrent
    two or more tasks (independent events) occur simultaneously (be): a program simultaneous execution of multiple independent tasks
    before the computer single-core cpu (central processing unit) a time can only perform one task: scheduling by the operating system, multiple times per second so-called task switching.
    Concurrent illusion (not being true concurrent); this switch (context switching) is a time overhead, such as a variety of operating system you want to save the state when you switch.
    The implementation schedule and other information, will take time, a time will switch back to the need to respond to such information.

    Hardware development, there has been a multiprocessor computer: for servers and high-performance computing.
    Desktop: multi-core (multiple) cpu on a single chip: dual-core, quad-core, eight-core ...
    then you really being able to perform multiple tasks (concurrent hardware);

    The reason for using concurrency: The main thing is that at the same time can do more to improve performance;

    (1.2) executable
    file on the disk, the next windows, a .exe extension.
    Under linux ls -la have rwxrwxrwx executable permissions.

    (1.3) Process: The executable program that can run
    under windows, double-click on an executable program to run
    under linux ./ filename ./a;
    an executable program up and running, called the creation of a process;
    the process is running up executable program;

    (1.4) Thread:
    A) each process (to implement an executable program), there is a main thread, the main thread is unique, that is, a process can have only one main thread.
    b) When you run an executable program, resulting in a process, the main thread with the process quietly started up.

    In fact, when you run the program, in fact, it is the main thread of the process to execute the code (call) the main function of;
    threads and processes go hand in hand, you have me, I have you.
    Thread: is used to execute the code.
    This thing can be understood as a thread, a code execution path.
    In addition to the main thread, we can create other threads through their own writing code, other threads taking the other road. Complete different purposes
    each creates a new thread, I can at the same time, do more things in a different (and more to follow a different code execution path)


    Multithreading (concurrent)
    thread is not possible, each thread need to buy a separate stack space, switching between threads need to save a lot of intermediate states;
    consuming the present time belongs to the program running;

    Summed up the thread:
    A thread is used to execute the code;
    to be understood as a code execution path thread this thing, a new thread is representative of a new path.
    A process of automatically includes a main thread, the main thread with the process of quietly up and running, we can create other threads (non-main thread) compiled by the code
    but the number is not recommended creation of more than 200 to 300, as the number of appropriate, We need to optimize the actual situation.
    Because the main thread is automatically activated, so a process in at least one thread (the main thread) processes and threads feeling is the relationship status and his son
    put it plainly: a multithreaded program can do more things at the same time, the operating efficiency is relatively high, but in the end how high, is not an easy thing to assess and quantify.
    We still need to be adjusted and optimized in real projects;
    (1.5) learning experience
    developing multi-threaded program: senior programmer is an essential skill.
    c ++ thread will involve relatively large number of new concepts, particularly critical for high salaries, not anxious.


    II: concurrent implementation


    Two or more tasks (independent events) occur simultaneously (be)
    means to achieve concurrency:
    A) We concurrency through multiple processes.
    b) In a separate process, I create multiple threads to achieve concurrency; write your own code to create other threads other than the main thread;
    (2.1) more complicated process
    after word after startup is a process, ie the browser is started a process
    account server, the communication between the game logic server, the server process.
    Communication between processes (on the same computer: pipes, files, message queues, shared memory);
                                 different computers: socket communication technology;

    (2.2) multithreading: a single process to create multiple threads.
    Thread: It feels like a lightweight process, each thread has its own independent operational path, but a process all threads share address space (shared memory);
    global variables, pointers, references can be passed in the thread, so : using multithreading far less than the cost of multiple processes.
    Shared memory brings new problems, data consistency problems: thread A, thread B;

    More complicated process and multi-threaded though can be mixed, but the teacher suggested to give priority to multi-threading technology, rather than multiple processes;

    This article is mainly about multi-threaded.

    (2.3) Summary:
    and processes than the thread has the following advantages:
    (1) the thread start faster, more lightweight;
    (2) cost less system resources, faster execution, this means of communication such as shared memory than almost any other means of communication.
    Shortcoming
    (a) the use of a certain degree of difficulty, need careful handling data consistency problems.

    Three: c ++ 11 standard thread library


    windows: CreateThread (), _beginthread ( ), _ beginthreadex () creates a thread
    linux: pthread_create () creates a thread
    critical section, mutex;
    previous multithreaded code can not be cross-platform;
    the POSIX the Thread (the pthread); cross-platform; it needs to be done some configuration, so use them it is not so easy.

    From c ++ 11 new standards, c ++ language itself adds support for multi-threading, which means portability (cross-platform); This greatly reduces the workload of developers.


  

Published 101 original articles · won praise 73 · views 120 000 +

Guess you like

Origin blog.csdn.net/usstmiracle/article/details/104130442