Im trying to compile the following code for an embedded device (Its a crosscompiler from TI with experimental support of C++11 (C++0)). Target:
arm-arago-linux-gnueabi Thread model: posix gcc version 4.5.3 20110311 (prerelease) (GCC)
The default specifier for the move constructor and move assignement operator cannot be compiled (/home/user/test/main.cpp:40:26: error: 'th& th::operator=(th&&)' cannot be defaulted
).
std::make_unique
& emplace_back
are not implemented, those not usable.
What do I need to change in the code to make it work for this platform?
class th {
public:
void func() {
sleep(3);
*this->progress = 100;
}
th(int* prog) :
progress(prog),
m_thread(std::thread(&th::func, this)) {};
th(th const& other) = delete;
th(th && other) = default;
th& operator=(th const& other) = delete;
th& operator=(th &&) = default;
void join() { m_thread.join(); }
int *progress;
private:
std::thread m_thread;
};
int main(void) {
std::vector<int> progress;
progress.push_back(-1);
progress.push_back(-1);
std::deque<std::unique_ptr<th>> deq;
std::cout << "progress[0]:" << progress[0] << std::endl;
std::cout << "progress[1]:" << progress[1] << std::endl;
std::cout << "executing threads..." << std::endl;
for(size_t i = 0; i < 2; ++i) {
deq.push_back(std::unique_ptr<th>(new th(&progress[i])));
}
while(true) {
std::cout << "SIZE:" << deq.size() << std::endl;
if(deq.size() == 0)
break;
for (std::deque<std::unique_ptr<th>>::iterator it = deq.begin(); it != deq.end(); it++) {
//std::cout << (*it)->progress << std::endl;
if(*((*it)->progress) == 100) {
std::cout << "JOIN & DELETE" << std::endl;
(*it)->join();
deq.erase(it);
}
else {
std::cout << "STILL RUNNING" << std::endl;
}
//std::cout << *((*it)->progress) << std::endl;
}
sleep(1);
}
exit(EXIT_SUCCESS);
}
gcc 4.5.x does not support the generation of move special member functions, see N3053 for more details, and also https://gcc.gnu.org/gcc-4.5/cxx0x_status.html for the gcc C++11 support features, so you're out of luck with the compiler. Your code compiles fine on gcc5/6 and clang, see it live here.
One option is to remove altogether the lines
and let the compiler do the work and generate the special member functions for you. BTW, the copy ctor and copy assignment operator will be deleted by default, as
std::thread
is non-copyable.