如何創建對象的std :: vector,並且每個對象都有一個boost :: thread封裝在裏面。std ::帶有boost :: thread封裝的對象向量insde
class INSTRUMENT {
public:
INSTRUMENT() : m_thread(new boost::thread(&INSTRUMENT::Start, this)) {
x = "abc";
}
~INSTRUMENT() {}
void Start();
public:
std::string x;
boost::shared_ptr<boost::thread> m_thread;
};
void INSTRUMENT::Start() {
try {
while (1) {
boost::this_thread::interruption_point();
std::cout << "here " << x << std::endl;
}
} catch (boost::thread_interrupted &thread_e) {
std::cout << "exit " << x << std::endl;
} catch (std::exception &e) {
}
}
std::vector<INSTRUMENT> m_inst_vector;
for (int i = 0; i < 5; i++) {
m_inst_vector.push_back(INSTRUMENT());
}
該代碼編譯得很好,但輸出只是一些垃圾,不像預期的「abc」。在調試中,我注意到每次調用.push_back()時都會調用〜INSTRUMENT()。
由於當前設計的限制,我嘗試不使用boost :: group_thread。只是想知道是否有可能有一個線程內的對象的std ::向量,或任何建議類似的設計將是非常有益的。
我在SO上找到類似的線程。它提到了編譯器支持的移動語義,但沒有解釋它是什麼。 How can I add boost threads to a vector
謝謝。
編輯我的答案。 – vz0 2012-02-23 05:10:12