我想等待一段時間的條件。 我讀了助推文檔,似乎最好使用帶謂詞的函數wait_for
,如here所述。boost :: condition_variable - 使用wait_for與謂詞
不幸的是,這個例子對我來說並不是很有用。我應該如何編寫謂詞?我試着寫上面報道的代碼,但在Visual Studio編譯器抱怨:c:\boost\boost\thread\win32\condition_variable.hpp(394): error C2064: term does not evaluate to a function taking 0 arguments
這是代碼的一部分:
class MyClass{
boost::mutex mutex;
boost::condition_variable myCondition;
//...
void foo();
bool myPredicate();
}
void MyClass::foo(){
boost::unique_lock<boost::mutex> lock(mutex);
boost::chrono::microseconds period(25000);
// ...
boost::chrono::system_clock::time_point wakeUpTime = boost::chrono::system_clock::now() + period;
if(myCondition.wait_until(lock,wakeUpTime,MyClass::myPredicate) == true){/...}
}
bool MyClass::myPredicate(){
if(...)
return true;
else
return true;
}
什麼用wait_for
與謂語的正確方法是什麼?
感謝這個完整的雁,所以基本上使用的最後兩個提出的方法之一,讓我來調用函數'比如說myPredicate()'在CURREN t對象。那是對的嗎?這允許我通過使用'myClass'中定義的變量來評估謂詞。 – Maverik
除了第一個('Foobar'),它們都可以訪問'myClass'中定義的實例變量。我建議使用_anonymous function_。它可以讓你將謂詞的部分內容聯繫在一起,並將屬於一起的東西彙集在一起。但是,我不確定此功能是否已被Visual Studio支持。 – nosid
是的,Visual Studio 2012支持它,代碼編譯。你知道有沒有辦法測試它?我的意思是,謂詞用於在虛假虛弱情況下避免錯誤。有沒有辦法模擬虛假的虛弱? – Maverik