Boost logo

Boost Users :

Subject: [Boost-users] help using async_wait
From: Lane (software.research.development_at_[hidden])
Date: 2015-11-11 19:44:26


I'm trying to have a file automatically close when a deadline_timer is
expired. Seeing that a file is no longer open, another file will get
created and a timer started.

But I'm having some issues understanding deadline_timer. I believe the
io.run() in start_timer blocks (thinking that async_wait shouldn't block),
so I tried to put it on it's own thread so that my main would continue to
loop and write to the file while a timer is running in the background which
would eventually close the file.

Anyone see where I'm going wrong?

---
void MyFileTimer::start_timer()
{
    ba::deadline_timer timer(io, boost::posix_time::seconds(10));
    timer.async_wait(boost::bind(&MyFileTimer::timer_expired, this, _1));
    t_thread = unique_ptr<boost::thread>(
        new boost::thread(boost::bind(&ba::io_service::run, &io)));
    io.run();
}
void MyFileTimer::timer_expired(const boost::system::error_code& e)
{
    // Closing file
    my_file.flush();
    my_file.close();
    io.reset();
    t_thread->join();
}
bool MyFileTimer::create_file()
{
    // ...
    string filename = my_unique_file_name;
    my_file.open(filename, ios::out);
    start_timer();
    return true;
}
--- snip of main ---
while (true) {
    if (!my_file.is_open()) {
        create_file();
    }
    my_file->write(str);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net