![]() |
Home | Libraries | People | FAQ | More |
在Boost 1.35.0,Boost.Thread 庫使用Boost.Date_Time庫提供超時處理。 包括 (但不限於):
對於那些接受完整時間做參數的函數,我們需要一個boost::system_time對象。 典型的, 可以通過 boost::get_system_time()得到當前時間,然後加上一個時間段得到, 比如:
boost::system_time const timeout=boost::get_system_time() + boost::posix_time::milliseconds(500);
extern bool done;
extern boost::mutex m;
extern boost::condition_variable cond;
boost::unique_lock<boost::mutex> lk(m);
while(!done)
{
if(!cond.timed_wait(lk,timeout))
{
throw "timed out";
}
}
對於接受時間段(TimeDuration)做參數的函數, 我們需要一個滿足Boost.Date_Time Time Duration requirements條件的對象,比如:
boost::this_thread::sleep(boost::posix_time::milliseconds(25));
boost::mutex m;
if(m.timed_lock(boost::posix_time::nanoseconds(100)))
{
// ...
}
#include <boost/thread/thread_time.hpp>
typedef boost::posix_time::ptime system_time;
參考Boost.Date_Time庫中boost::posix_time::ptime的文檔。
#include <boost/thread/thread_time.hpp>
system_time get_system_time();
當前時間。
無。