Study notes "C ++ concurrent programming combat" (2) - thread management

Wait for the thread to complete: 
    join; only be invoked once, in addition to the best call joinable test before the call is callable; moreover join after calling thread is unavailable, the joinable fanhuifalse. 
    
Detached thread: 
    the detach: general as a thread running in the background, sometimes still useful. But the need to address the issue held by some thread resources, life cycle, you can use a long resource life cycle 
    or a copy of the resource to the thread of the stack rather than using shared resources. After calling detach the thread execution thread object will no longer be associated with can not be added. 
    Also, it is best to call before the call to test whether joinable callable. 

Constructors: 
    When the constructor passing parameters, using the default parameters copied to the internal thread stack space, unexpected results may occur. At this point it can be used std :: ref wrapper object (in fact, 
    is made wrapped object address) in order to achieve the purpose of the internal thread external reference parameter object rather than duplicate copies of copies. 
 
Transfer of ownership of threads: 
    thread thread support transfer of ownership, that is ownership of a particular thread of execution can move between thread object instance. That support std :: move semantics or direct assignment instance of 
    an object (the object should be assigned to a temporary variable object is available directly assigned or need to use std :: move semantics). 
    Also not subject to an existing thread thread thread title and then get ownership of the new thread other objects that could cause the process to terminate (it is possible to increase the joinable test). 

Select the number of threads running time:
    Take full advantage of multi-core processor hardware concurrency, std :: thread :: hardware_currency get the current number of cores (may return 0) the system CPU. 
    
A thread identifier: 
    STD Thread :: :: id (packaging a thread identification value id), can obtain an identifier of the current thread :: thread :: get_id () or std :: this_thread :: get_id () by thread object std. 
    This identifier id is sometimes identified as the distinction of different threads. When can perform or not perform certain actions in checking a thread, or useful in debugging logs.
    

 

Guess you like

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