3
我有以下代碼(這是一些半須藤代碼,這可能不是編譯):如何從boost線程訪問我的類實例?
class FooBar {
public:
void a();
void b();
boost::shared_ptr<boost::thread> m_thread;
std::string m_test;
};
void FooBar::a() {
m_test = "Foo bar"
m_thread = shared_ptr<thread>(new thread(bind(&FooBar::b, this)));
}
void FooBar::b() {
cout << m_test;
}
代碼cout << test
不產生任何輸出,因爲m_test
是""
代替"Foo bar"
。爲什麼是這樣?我認爲通過this
作爲bind
的第二個參數將允許我從b()
訪問相同的實例 - 我錯了嗎?
非常感謝發佈工作代碼!是的,我在析構函數中放置了一個斷點,然後嘿,直到我用靜態單例的完整類型而不是指針 - 噢!所以每次我打電話給單身人士時都會創建一個新實例。現在修復,再次感謝您的代碼。 – 2009-12-01 17:50:49