Example #1
0
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 + Clock::duration(250);
    while (test2 == 0 && cv.wait_until(lk, t) == boost::cv_status::no_timeout) {}
    Clock::time_point t1 = Clock::now();
    if (runs == 0)
    {
      ns d = t1 - t0;
      BOOST_THREAD_TEST_IT(d, ns(max_diff));
      assert(test2 != 0);
    }
    else
    {
      ns d = t1 - t0 - Clock::duration(250);
      BOOST_THREAD_TEST_IT(d, ns(max_diff));
      assert(test2 == 0);
    }
    ++runs;
  } catch(...) {
      assert(false);
      std::cout << "ERROR exception" << __LINE__ << std::endl;
  }
}
Example #2
0
void f()
{
  boost::unique_lock < boost::mutex > lk(mut);
  BOOST_TEST(test2 == 0);
  test1 = 1;
  cv.notify_one();
  Clock::time_point t0 = Clock::now();
  Clock::time_point t = t0 + Clock::duration(250);
  int count=0;
  while (test2 == 0 && cv.wait_until(lk, t) == boost::cv_status::no_timeout)
    count++;
  Clock::time_point t1 = Clock::now();
  if (runs == 0)
  {
    BOOST_TEST(t1 - t0 < Clock::duration(250));
    BOOST_TEST(test2 != 0);
  }
  else
  {
    // This test is spurious as it depends on the time the thread system switches the threads
    BOOST_TEST(t1 - t0 - Clock::duration(250) < Clock::duration(count*250+5+1000));
    BOOST_TEST(test2 == 0);
  }
  ++runs;
}
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));
}
void f()
{
  boost::unique_lock<boost::mutex> lk(mut);
  BOOST_TEST(test2 == 0);
  test1 = 1;
  cv.notify_one();
  Clock::time_point t0 = Clock::now();
  Clock::time_point t = t0 + Clock::duration(250);
  bool r = cv.wait_until(lk, t, Pred(test2));
  Clock::time_point t1 = Clock::now();
  if (runs == 0)
  {
    BOOST_TEST(t1 - t0 < Clock::duration(250));
    BOOST_TEST(test2 != 0);
    BOOST_TEST(r);
  }
  else
  {
    BOOST_TEST(t1 - t0 - Clock::duration(250) < Clock::duration(250+2));
    BOOST_TEST(test2 == 0);
    BOOST_TEST(!r);
  }
  ++runs;
}