本文整理汇总了C++中boost::condition_variable::wait_for方法的典型用法代码示例。如果您正苦于以下问题:C++ condition_variable::wait_for方法的具体用法?C++ condition_variable::wait_for怎么用?C++ condition_variable::wait_for使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::condition_variable
的用法示例。
在下文中一共展示了condition_variable::wait_for方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: f
void f()
{
try {
boost::unique_lock < boost::mutex > lk(mut);
assert(test2 == 0);
test1 = 1;
cv.notify_one();
Clock::time_point t0 = Clock::now();
cv.wait_for(lk, milliseconds(250), Pred(test2));
Clock::time_point t1 = Clock::now();
if (runs == 0)
{
assert(t1 - t0 < max_diff);
assert(test2 != 0);
}
else
{
assert(t1 - t0 - milliseconds(250) < max_diff);
assert(test2 == 0);
}
++runs;
} catch(...) {
std::cout << "ERROR exception" << __LINE__ << std::endl;
assert(false);
}
}
示例2: f
void f()
{
try {
boost::unique_lock<boost::mutex> lk(mut);
assert(test2 == 0);
test1 = 1;
cv.notify_one();
Clock::time_point t0 = Clock::now();
Clock::time_point t = t0 + milliseconds(250);
while (test2 == 0 && cv.wait_for(lk, t - Clock::now()) == boost::cv_status::no_timeout) {}
Clock::time_point t1 = Clock::now();
if (runs == 0)
{
assert(t1 - t0 < max_diff);
assert(test2 != 0);
}
else
{
nanoseconds d = t1 - t0 - milliseconds(250);
std::cout << "diff= " << d.count() << std::endl;
std::cout << "max_diff= " << max_diff.count() << std::endl;
assert( d < max_diff);
assert(test2 == 0);
}
++runs;
} catch(...) {
std::cout << "ERROR exception" << __LINE__ << std::endl;
assert(false);
}
}
示例3: basic_examples
void basic_examples()
{
std::cout << "Running basic examples\n";
using namespace boost;
using namespace boost::chrono;
system_clock::time_point time_limit = system_clock::now() + seconds(4) + milliseconds(500);
this_thread::sleep_for(seconds(3));
this_thread::sleep_for(nanoseconds(300));
this_thread::sleep_until(time_limit);
// this_thread::sleep_for(time_limit); // desired compile-time error
// this_thread::sleep_until(seconds(3)); // desired compile-time error
mut.try_lock_for(milliseconds(30));
mut.try_lock_until(time_limit);
// mut.try_lock_for(time_limit); // desired compile-time error
// mut.try_lock_until(milliseconds(30)); // desired compile-time error
cv.wait_for(m, minutes(1)); // real code would put this in a loop
cv.wait_until(m, time_limit); // real code would put this in a loop
// For those who prefer floating point
this_thread::sleep_for(duration<double>(0.25));
this_thread::sleep_until(system_clock::now() + duration<double>(1.5));
}
示例4: pop
int pop(EventImpl **result, unsigned int timeoutMilliseconds)
{
if (0 == timeoutMilliseconds) {
while (0 != tryPop(result)) {
boost::unique_lock<boost::mutex> g(d_eventQueueMutex);
d_hasEvent.wait(g);
}
return 0;
}
if (0 == tryPop(result)) {
return 0;
}
{
boost::unique_lock<boost::mutex> g(d_eventQueueMutex);
d_hasEvent.wait_for(g,
boost::chrono::milliseconds(timeoutMilliseconds));
}
return tryPop(result);
}