我試圖爲嵌入式設備編譯下面的代碼(它是TI提供的具有C++ 11(C++ 0)實驗支持的交叉編譯器)。目標:移動構造函數並移動類
臂阿拉戈-Linux的gnueabi線程模型:POSIX gcc版本4.5.3 20110311 (搶鮮)(GCC)
的移動構造函數的默認符和移動assignement操作員無法編譯(/home/user/test/main.cpp:40:26: error: 'th& th::operator=(th&&)' cannot be defaulted
)。
std::make_unique
& emplace_back
沒有實現,那些不可用。
我需要什麼,在代碼改變,使之成爲這個平臺的工作?
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);
}
謝謝@vsoftco!有什麼辦法創建一個動態容器來添加和刪除線程與GCC 4.5? – marc
@mmmint如何讓編譯器生成除你自定義的'th(int *)'之外的所有ctors?換句話說,擺脫你默認/刪除特殊成員函數的4行。 – vsoftco