本文整理汇总了C++中std::condition_variable_any::wait_for方法的典型用法代码示例。如果您正苦于以下问题:C++ condition_variable_any::wait_for方法的具体用法?C++ condition_variable_any::wait_for怎么用?C++ condition_variable_any::wait_for使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::condition_variable_any
的用法示例。
在下文中一共展示了condition_variable_any::wait_for方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: f
void f()
{
typedef std::chrono::system_clock Clock;
typedef std::chrono::milliseconds milliseconds;
L1 lk(m0);
assert(test2 == 0);
test1 = 1;
cv.notify_one();
Clock::time_point t0 = Clock::now();
while (test2 == 0 &&
cv.wait_for(lk, milliseconds(250)) == std::cv_status::no_timeout)
;
Clock::time_point t1 = Clock::now();
if (runs == 0)
{
assert(t1 - t0 < milliseconds(250));
assert(test2 != 0);
}
else
{
assert(t1 - t0 - milliseconds(250) < milliseconds(5));
assert(test2 == 0);
}
++runs;
}
示例2: waits
void waits(int idx)
{
std::unique_lock<std::mutex> lk(cv_m);
if(cv.wait_for(lk, std::chrono::milliseconds(idx*100), [](){return i == 1;}))
std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n';
else
std::cerr << "Thread " << idx << " timed out. i == " << i << '\n';
}
示例3: unlock
void unlock() {
if ( --counter == 0 ) {
cv.notify_all();
} else {
std::unique_lock<sync_object_type> barrier_lock(sync);
while(counter != 0)
cv.wait_for(barrier_lock, std::chrono::milliseconds(1));
}
}
示例4: catch
int
main()
{
std::set_terminate(f1);
try
{
std::thread(signal_me).detach();
mut.lock();
cv.wait_for(mut, std::chrono::milliseconds(250));
}
catch (...) {}
assert(false);
}
示例5: f
void f()
{
typedef std::chrono::system_clock Clock;
typedef std::chrono::milliseconds milliseconds;
L1 lk(m0);
assert(test2 == 0);
test1 = 1;
cv.notify_one();
Clock::time_point t0 = Clock::now();
bool r = cv.wait_for(lk, milliseconds(250), Pred(test2));
Clock::time_point t1 = Clock::now();
if (runs == 0)
{
assert(t1 - t0 < milliseconds(250));
assert(test2 != 0);
}
else
{
assert(t1 - t0 - milliseconds(250) < milliseconds(5));
assert(test2 == 0);
}
++runs;
}