c++ - std::thread constructor with no parameter -
according cppreference.com, std::thread
constructor no parameter means:
creates new thread object not represent thread.
my questions are:
- why need constructor? , if create
thread
using constructor, how can "assign" thread function later? - why don't have "run(function_address)" method when constructed no parameter, can specify function "run"
thread
. - or, can construct
thread
callable parameter (function, functors, etc.) call "run()" method execute thread later. whystd::thread
not designed in way?
your question suggests there might confusion , helpful separate ideas of thread of execution std::thread
type, , separate both idea of "thread function".
- a thread of execution represents flow of control through program, corresponding os thread managed kernel.
- an object of type
std::thread
can associated thread of execution, or can "empty" , not refer thread of execution. - there no such concept "thread function" in standard c++. function can run in new thread of execution passing constructor of
std::thread
object.
- why need constructor?
to construct empty state doesn't refer thread of execution. might want have member variable of class std::thread
, not want associate thread of execution right away. default construct it, , later launch new thread of execution , associate std::thread
member variable. or might want do:
std::thread t; if (some_condition) { t = std::thread{ func1, arg1 }; } else { auto result = some_calculation(); t = std::thread{ func2, arg2, result }; }
the default constructor allows object t
created without launching new thread of execution until needed.
and if create thread using constructor, how can "assign" thread function later?
you "assign" using "assignment" :-)
but don't assign "thread function" it, not std::thread
for. assign std::thread
it:
std::thread t; std::thread t2{ func, args }; t = std::move(t2);
think in terms of creating new thread of execution not "assigning thread function" something. you're not assigning function, that's std::function
used for. requesting runtime create new thread of execution, managed std::thread
object.
- why don't have "run(function_address)" method when constructed no parameter, can specify function "run" thread.
because don't need it. start new threads of execution constructing std::thread
object arguments. if want thread of execution associated existing object can move-assigning or swapping.
- or, can construct thread callable parameter(function, functors, etc.) call "run()" method execute thread later. why std::thread not designed in way?
why should designed way?
the std::thread
type managing thread of execution not holding callable object later use. if want create callable object can later run on new thread of execution there lots of ways in c++ (using lambda expression, or std::bind
, or std::function
, or std::packaged_task
, or custom functor type). job of std::thread
manage thread of execution not hold onto callable object until want call it.
Comments
Post a Comment