1
    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) {
 //   some work
          }
   }
   public:
   worker(const std::string& worker_name)
       : done(false) {
        // some initialization 
    }
 // other fields
 };


  error: use of deleted function 'std::atomic_bool::atomic_bool(const std::atomic_bool&)'

GCC 4.9

as I see atomic can't be copied, just moved. code above requires copying ctor for some_object class. how to solve this ?

(probably the design itself is worse, here , some_object is a functor for a thread, and atomic is a flag to shut the process down)

amigo421
  • 2,429
  • 4
  • 26
  • 55
  • You could create a `back_emplacer` (see, for example, http://stackoverflow.com/questions/18728257/back-emplacer-implementation-default-operator-vs-universal-reference-version) – Andrew Sep 30 '15 at 15:12
  • 1
    Can you provide a [minimal, complete, and verifiable example](http://www.stackoverflow.com/help/mcve)? The problem is likely in your "..." somewhere. Is `some_object` movable? – Barry Sep 30 '15 at 15:22
  • 1
    What compiler? What line of the above code is complaining about the copy ctor being called? What is the actual error message, in entirety? Why do you think "the above code" requires "copying ctor" exactly? – Yakk - Adam Nevraumont Sep 30 '15 at 15:37

1 Answers1

5

Your class:

class worker {
    std::atomic_bool done;
};

is not copyable or moveable because std::atomic explicitly deletes its copy constructor (which implicitly deletes its move constructor). If you want to allow it to be moved or copied, you have to write that yourself, e.g.:

worker(worker&& rhs)
: done(rhs.done.load())
{ }
Barry
  • 286,269
  • 29
  • 621
  • 977