thread::timed_join
使用需要绝对时间(即时间点)而不是持续时间的重载。将绝对时间期限设为当前时间加上您想要的任何超时时间。这将确保thread::timed_join
循环中的所有调用都不会等待超过绝对时间期限。
在 Boost.Thread 的最新版本中(从 Boost 1.50 开始),Boost.Date_Time 现在已弃用,取而代之的是Boost.Chrono。这是为了更接近C++11中std::thread的 API 。
此示例显示如何使用 Boost.Chrono 或 Boost.DateTime 指定绝对时间期限:
using namespace boost;
#if BOOST_VERSION < 105000
// Use of Boost.DateTime in Boost.Thread is deprecated as of 1.50
posix_time::ptime deadline =
posix_time::microsec_clock::local_time() +
posix_time::seconds(timeoutSeconds);
#else
chrono::system_clock::time_point deadline =
chrono::system_clock::now() + chrono::seconds(timeoutSeconds);
#endif
for(int i = 0; i < number_of_threads; ++i)
{
threads[i]->timed_join(deadline);
}
文档中的此页面显示了 Boost.Date_Time 示例用法。
文档中的这个页面是关于 Boost.Chrono 的教程。