To avoid confusion with thread represents
std::thread
its object instance, use the thread indicates that the thread under the operating system concepts
Chapter 2 thread
Management
2.1 thread
created (constructor)
a. default constructor
default: thread() noexcept;
Create a placeholder, and is not associated with any thread. Using a scene defined as thread
an array. After passing through it can thread& operator=( thread&& other ) noexcept
be 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;
thread
Objects can not copy
2.2 join
: wait for the thread to complete execution
join
It will also clean up all the memory and thread-related. join
After returning thread
is 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 join
before the end of the call, in order to avoid this, we need to catch
also call join
. But try-catch way too complicated and would clutter scope. If there is thus to ensure that all exit paths are in join
demand, can be employed RAII mechanism, a package thread_guard class, in which the processing destructor
2.3 detach
: a background thread
To thread
object calls detach
will thread associated with it running in the background, and cut off any means able to communicate, and no longer by thread
reference it an object, and therefore can not bejoin
Detach thread is also called daemon threads
thread
Whether an object can be detach
and whether it can be join
the condition required to meet the same: to be associated with the thread, the reflected joinable()
must returntrue
2.4 thread
parameter passing
Structure thread
after 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 buf
be 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 thread
the object is detach
, the buf
point 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 thread
Transfer of ownership
thread
Examples 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::id
can be compared, hashable, but its value is no semantic meaning (meaningless)
Call the thread
object 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