c++ - boost asio asynchronously waiting on a condition variable -
is possible perform asynchronous wait (read : non-blocking) on conditional variable in boost::asio ? if isn't directly supported hints on implementing appreciated.
i implement timer , fire wakeup every few ms, approach vastly inferior, find hard believe condition variable synchronization not implemented / documented.
if understand intent correctly, want launch event handler, when condition variable signaled, in context of asio thread pool? think sufficient wait on condition variable in beginning of handler, , io_service::post() in pool in end, of sort:
#include <iostream> #include <boost/asio.hpp> #include <boost/thread.hpp> boost::asio::io_service io; boost::mutex mx; boost::condition_variable cv; void handler() { boost::unique_lock<boost::mutex> lk(mx); cv.wait(lk); std::cout << "handler awakened\n"; io.post(handler); } void buzzer() { for(;;) { boost::this_thread::sleep(boost::posix_time::seconds(1)); boost::lock_guard<boost::mutex> lk(mx); cv.notify_all(); } } int main() { io.post(handler); boost::thread bt(buzzer); io.run(); }
Comments
Post a Comment