int main() { { L1 lk(m0); boost::thread t(f); BOOST_TEST(test1 == 0); while (test1 == 0) cv.wait(lk); BOOST_TEST(test1 != 0); test2 = 1; lk.unlock(); cv.notify_one(); t.join(); } test1 = 0; test2 = 0; { L1 lk(m0); boost::thread t(f); BOOST_TEST(test1 == 0); while (test1 == 0) cv.wait(lk); BOOST_TEST(test1 != 0); lk.unlock(); t.join(); } return boost::report_errors(); }
void put(int x) { { boost::mutex::scoped_lock lock(mu); while (is_full()) { { boost::mutex::scoped_lock lock(io_mu); cout << "full waiting..." << endl; } cond_put.wait(mu); } stk.push(x); ++un_read; } cond_get.notify_one(); }
void get(int *x) { { boost::mutex::scoped_lock lock(mu); while (is_empty()) { { boost::mutex::scoped_lock lock(io_mu); cout << "empty waiting..." << endl; } cond_get.wait(mu); } --un_read; *x = stk.top(); stk.pop(); } cond_put.notify_one(); }
void f() { typedef boost::chrono::system_clock Clock; typedef boost::chrono::milliseconds milliseconds; L1 lk(m0); BOOST_TEST(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) { BOOST_TEST(t1 - t0 < milliseconds(250)); BOOST_TEST(test2 != 0); } else { BOOST_TEST(t1 - t0 - milliseconds(250) < milliseconds(250+5)); BOOST_TEST(test2 == 0); } ++runs; }
void f() { L1 lk(m0); 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; }
void f() { L1 lk(m0); BOOST_TEST(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) { ns d = t1 - t0; BOOST_THREAD_TEST_IT(d, ns(max_diff)); BOOST_TEST(test2 != 0); } else { ns d = t1 - t0 - ms(250); BOOST_THREAD_TEST_IT(d, ns(max_diff)); BOOST_TEST(test2 == 0); } ++runs; }
void notify_event(const Ctrl& ctrl, uint32_t devnum=0) { scoped_lock lock(guard_); shared_ptr<event_ctrl<Ctrl> > p(new event_ctrl<Ctrl>(ctrl, devnum)); eventQueue_.push(p); wakeup_.notify_one(); }
// listenerを中断する void stop_listener() { quit_ = true; wakeup_.notify_one(); }