Study notes "C ++ concurrent programming combat" (4) - synchronized concurrent operation

Waiting for an event: 
    a condition variable Wait: 
        STD :: condition_variable / STD :: condition_variable_any, std :: mutex with the former and std :: unique_lock, std :: lock_guard, 
    which can be used with similar mutex (including mutex) type work, but because it has an internal std :: lock will have a price on protected members of the mutex, performance and size. 
    Use wait wait condition variable conditions are met in order to proceed, notify_one notice any waiting thread is executing a wait may continue, notify_all notify all waiting 
    threads can proceed. 
    
    Wait future: 
        a disposable event; i.e., when the future become ready waiting state, its state can not be reset. 
    Future offers two, the only one kind of future (std :: future), another shared future (std :: shared_future), Examples of the former point to an event, a plurality of shared latter 
    instance may be associated with the same event. For the latter when the event is ready, all instances become ready. 
    
    Asynchronous get backstage results: 
        std :: async start an asynchronous task, it returns a std :: future object that will hold the return value of the function; this time to call  
    the object's get interfaces (thread will be blocked until the future objects and get ready to return value). std :: future template parameter is the type to get the value returned.
    std :: async overloaded version supports the specified boot mode, std Launch deferreds :: :: / :: std :: Launch the async, the former is delayed until the call wait /run get, the latter immediately open 
    a new thread to run, as if the combination is selected with a platform specific implementation. 
    std :: async can initiate an asynchronous task, in addition packaged_task :: std <>, std :: Promise <> can also be achieved task starts running in parallel. 
    std :: async asynchronous operations on a higher level, so that we do not focus on the details of the internal thread creation, can easily access asynchronous execution status and results. 
    
        :: packaged_task std <> bind a future to a function or callable on an object, template parameters for the function signature. std :: packaged_task overloaded operator (); 
    you can call to perform the task, then call the object's interfaces get_future in other places, you can get to std :: future associated with the object; :: Promise relatively std <> , the former Save is 
    a callable function or object (to achieve asynchronous operation), which is a shared state value; get_future common that are acquired by the object associated with the future, in order to obtain a final 
    call a function or object returns a value or set of value. 
        
        :: Promise STD <>An asynchronous service provider, by setting worthy way that std :: future object is associated to read setting values. set_value promise in the interface 
    , or may be set such that abnormality set_exception std :: future become ready. The same std :: promise to future interfaces get_future get associated objects. 
    std :: future value or exception may be maintained. Such values provided promise in different places, to obtain the another place by a future target setting value. 
    
    :: shared_future shared STD: 
        STD :: shared_future may have multiple instances of a state associated with a reference event, can be copied, std :: future having mobility, i.e. valid after moving it returns invalid. 
    The future movement of the object is assigned becomes effective, in addition to std :: shared_future callable get acquires a shared state many times. std :: future can be converted to std :: shared_future object is 
    std :: future will become invalid after the transfer. 
    
Based on the time limit of waiting: 
    Chrono: Time Library, which offers the concept of time-related class templates or tools. They are: DURATION period, time point time_point, clock Clock; 
    Clock clock: system_clock (system clock (not uniform, variable)), steady_clock (stabilizing / average speed clock, immutable), high_resolution_clock (high-precision clock, 
    may be average speed clock or the system clock). 
    Time duration: duration<T type, ratio STD :: <N, M >> , storage time T is a template parameter type, parameter ration was expressed as a fraction of each period is expressed in units of a second, where 
    N / M is a value expressed as a number of seconds time segments, such as < 3600 , 1 > represents 3,600 seconds i.e. 1 hour time period, < 1 , 1000 > represents 0.001 seconds i.e. 1 ms time period, the standard library has provided 
    some common predefined period of time such as std: : chrono :: milliseconds, std :: chrono :: hours and so on. 
    Chrono tool transfer function :: :: duration_cast STD <> can achieve significant conversion between a period of time. 
    Time time_point: std :: chrono :: time_point. 
    
    C ++ accept timeout related functions are mainly: 
        std :: :: sleep_for this_thread, std :: :: this_thread sleep_until; 
        std :: :: wait_for condition_variable, std :: :: wait_until condition_variable;
        :: :: wait_for condition_variable_any std, std :: :: condition_variable_any wait_until;  
        std :: :: timed_mutex try_lock_for, std :: timed_mutex :: try_lock_until;
        std :: :: unique_lock unique_lock, std :: :: try_lock_for unique_lock, unique_lock std :: :: try_lock_until; 
        std :: :: wait_for Future, std: : Future :: wait_until; 
        STD :: :: wait_for shared_future, STD :: :: shared_future wait_until; 

ways to avoid data sharing variable: 
    using functional programming (FP), the communication sequence processing (the CSP / the MPI message passing interface) ; 
        
some operations to simplify the code synchronization: 
    functional programming with the future: means std :: future, std :: async and std :: thread or packaged_task concurrent operation can be achieved. 
    Operating a synchronous messaging: using multithreading divided performing different tasks, no shared data among threads, the communication between threads is achieved by sharing the message queue, 
    in order to divide concerns multithreading, simplifying system design and implementation of concurrency task.

 

Guess you like

Origin www.cnblogs.com/haomiao/p/11647397.html