multithreading - Why c++ threads are movable but not copiable? -
as title of question says, why c++ threads (std::thread
, pthread
) movable not copiable? consequences there, if make copiable?
regarding copying, consider following snippet:
void foo(); std::thread first (foo); std::thread second = first; // (*)
when line marked (*)
takes place, presumably of foo
executed. expected behavior be, then? execute foo
start? halt thread, copy registers , state, , rerun there?
in particular, given function objects part of standard, it's easy launch thread performs same operation earlier thread, reusing function object.
there's not motivation begin this, therefore.
regarding moves, though, consider following:
std::vector<std::thread> threads;
without move semantics, problematic: when vector needs internally resize, how move elements buffer? see more on here.
Comments
Post a Comment