|
Boost : |
From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2006-03-23 06:42:04
Hi Vaclav,
--- Vaclav Vesely <vaclav.vesely_at_[hidden]> wrote:
> Thank you, it helped me much.
No problem!
> I've written ASIO background_thread class which calls
> arbitrary functions in a background thread. If you find it
> useful, you can add it to examples. After some refining it may
> be even added to the ASIO library.
Yes, thanks, this use case could make a good example.
> Moreover background_thread has async_run_loop member function
> which calls the work function as long as the complete handler
> returns true. It's common usage of asynchronous classes (for
> example sockets, where handler for async_read will probably
> start a new read operation). It would be handy to cover this
> technique generally for all ASIO classes.
Actually I think there may be a way to simplify your code here -
assuming I correctly understand what you're trying to do.
The code seems to use the data members m_has_work and
m_has_work_condition to keep restarting the demuxer::run()
function whenever it has new work to do. The worker thread waits
on the condition if the demuxer doesn't have any work to do.
However I think this is duplicating what is already done for you
inside the demuxer. You should be able to achieve the same thing
by giving the m_work_demuxer some additional work to do. This
work will keep the m_work_demuxer's run() function going even if
there is nothing else to do. What I mean is something like:
- add a new auto_ptr<asio::demuxer::work> data member to the
background_thread class.
- in the background_thread constructor, initialise the data
member like so:
m_dummy_work(new asio::demuxer::work(m_work_demuxer))
- in the background_thread destructor, destroy the work and wait
for the thread to exit:
m_dummy_work.reset();
m_work_thread->join();
With this change, the work_thread_proc function only needs to
call demuxer::run(), and you no longer need the m_has_work,
m_has_work_condition and m_shutdown data members.
Cheers,
Chris
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk