c++ - std::generate_n algorithm requires copying ctor. how to avoid this in code below? -
std::vector<std::thread> thread_pool; ... std::generate_n(std::back_inserter(thread_pool), cpu_cores, [] (){ //... return std::thread{worker(worker_name) }; } );
where:
class worker { std::atomic_bool done; protected: void operator()() { while(!done) { // work } } public: worker(const std::string& worker_name) : done(false) { // initialization } // other fields }; error: use of deleted function 'std::atomic_bool::atomic_bool(const std::atomic_bool&)'
gcc 4.9
as see atomic can't copied, moved. code above requires copying ctor some_object class. how solve ?
(probably design worse, here , some_object functor thread, , atomic flag shut process down)
your class:
class worker { std::atomic_bool done; };
is not copyable or moveable because std::atomic
explicitly delete
s copy constructor (which implicitly delete
s move constructor). if want allow moved or copied, have write yourself, e.g.:
worker(worker&& rhs) : done(rhs.done.load()) { }
Comments
Post a Comment