My program has a class similar to the following:
class IOWorker {
std::thread thread_;
boost::asio::io_service ios_;
boost::optional<boost::asio::io_service::work> work_;
Callback callback_;
static thread_local IOWorker* this_thread_worker_;
public:
IOWorker(Callback cb);
~IOWorker();
// IO worker threads will call this to fetch their instance
static IOWorker* getIOWorkerInstance (void) {
return this_thread_worker_;
}
};
IOWorker::IOWorker (Callback cb) : callback_{std::move(cb)}
{
work_ = boost::in_place(boost::ref(ios_));
thread_ = std::thread{[this] () {
this_thread_worker_ = this;
ios_.run();
}};
}
Main thread calls the following function to create IOWorker
instances/threads:
std::vector<IOWorker> startIOWorkers(Callback cb)
{
std::vector<IOWorker> launched_workers;
launched_workers.reserve(10);
for (int i = 0; i < 10; ++i) {
launched_workers.emplace_back(cb);
}
return launched_workers;
}
But the compilation fails with the following error (pointing to _Up
):
error: call to implicitly-deleted copy constructor of 'IOWorker'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Not sure what changes I need to make in the above class to resolve this error.
I want this class (objects) to be non-copyable, but can be movable.