|
Boost Users : |
Subject: Re: [Boost-users] help using async_wait
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2015-11-17 00:44:21
On 12/11/2015 13:44, Lane wrote:
> 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?
I think the better question is: where are you going right? And the
answer is: nowhere. I can't see anything in that code that could
actually work.
Firstly, fstreams are not threadsafe, so closing one on another thread
without any synchronisation with the main thread is bad.
Secondly, starting a thread that runs the service and then also running
the service will result in it running on two threads. Which is fine as
far as io_service itself is concerned but is probably not what you
intended, and can cause issues down the line if you're not expecting the
async callbacks to happen on either thread concurrently.
Thirdly, trying to delete the io_service and join the thread within a
callback on the same io_service is just a complete no-no. Typically the
io_service should have a similar lifetime to the application, not to a
single operation.
If you're wanting to open a new file after a certain amount of time, you
should either use an existing library that supports log rotation, or
just track the file open time in your main loop and close/reopen the
file if the next write time is a certain distance from the open time.
No asynchronous code needed.
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