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 deletes copy constructor (which implicitly deletes move constructor). if want allow moved or copied, have write yourself, e.g.:

worker(worker&& rhs) : done(rhs.done.load()) { } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -