C ++ Concurrency in Action study notes

To avoid confusion with thread represents std::threadits object instance, use the thread indicates that the thread under the operating system concepts

Chapter 2 threadManagement

2.1 threadcreated (constructor)

a. default constructor

default: thread() noexcept;

Create a placeholder, and is not associated with any thread. Using a scene defined as threadan array. After passing through it can thread& operator=( thread&& other ) noexceptbe assigned a value associated with the thread and

b. Move Constructor

thread (thread&& other) noexcept;

c. initialization constructor

template<class Function, class...Args>
explicit thread(Function&& f, Args&&...args);

Common constructor, passing function and its parameters when creating the object, and thus the associated thread

d. Copy constructor

thread(const thread&) = delete;

threadObjects can not copy

2.2 join: wait for the thread to complete execution

joinIt will also clean up all the memory and thread-related. joinAfter returning threadis no longer associated with any thread, joinable()it will return false. Each thread can only be called oncejoin

May make an exception in the program joinbefore the end of the call, in order to avoid this, we need to catchalso call join. But try-catch way too complicated and would clutter scope. If there is thus to ensure that all exit paths are in joindemand, can be employed RAII mechanism, a package thread_guard class, in which the processing destructor

2.3 detach: a background thread

To threadobject calls detachwill thread associated with it running in the background, and cut off any means able to communicate, and no longer by threadreference it an object, and therefore can not bejoin

Detach thread is also called daemon threads

threadWhether an object can be detachand whether it can be jointhe condition required to meet the same: to be associated with the thread, the reflected joinable()must returntrue

2.4 threadparameter passing

Structure threadafter it passed callable immediately passed the required parameters when the object

Parameters are copied to the new thread can be created to access internal storage, and then as the right values ​​to the callable, as temporary variables as

If passed since the implicit transformation parameters, the result may not be expected. As for the argument string const&, it is passed in char buff[LEN]time, though it appears to bufbe an implicit transition string const, and then passed to callable, but the actual parameters are uploaded as is, that is not addressed, first passed in parameter (here buf(pointer / array name)) copied to an internal memory, then if threadthe object is detach, the bufpoint of the storage space to be cleaned, and at this time may not be enough time parameter callable constructed so undefined behavior will

Direct transfer non-const reference would then compile error, because the type constructor parameter values ​​is acceptable to the right, if you want to pass references requiredstd::ref

If the argument can not be copied only be move, after the transfer control parameters will be transferred

2.5 threadTransfer of ownership

threadExamples of objects that can be move, may be configured to move

You can not move to an already associated with the threadthread

2.6 std::thread::hardware_concurrency(): Usually the number of CPU cores, but only hint value, possibly zero

2.7 identifies thread

A thread identifier type: std::thread::idcan be compared, hashable, but its value is no semantic meaning (meaningless)

Call the threadobject instance get_id()method to get id. If no thread associated with the default configuration of the id return, represents not any thread

Id of the current thread can std::this_thread::get_id()get id

Guess you like

Origin www.cnblogs.com/jerrywossion/p/11324562.html
Recommended