Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] multi-threaded io_service::run produce wrong result
From: Lars Viklund (zao_at_[hidden])
Date: 2011-03-29 17:56:39


On Tue, Mar 29, 2011 at 06:49:12AM +0800, leiming wrote:
> 在 Tue, 29 Mar 2011 02:40:48 +0800,Lars Viklund <zao_at_[hidden]> 写道:
>
>> On Mon, Mar 28, 2011 at 08:40:02PM +0800, leiming wrote:
>>> Hi, there.
>>
>> An io_service will consider itself finished when there is no more
>> pending tasks to perform.
>>
> Thank you for your reply!
>
>> That means that if there is sufficient time between the first task and
>> the second task being posted, they may return from run() and end their
>> threads, resulting in the joins being completed.
> I'm also run io_service::run in the main thread after add all jobs but
> it still produce a wrong answer.
> It will finish all jobs even if the 2 threads ended because the jobs will
> run in the main thread after all jobs were added I thought.
> I don't know why

I do not quite know why, but I would guess that once an io_service has
gone into the stopped state, you might have to perform some action to
reset it, so that it can be started again. I would not expect any
actions you post to it during the stopped-but-not-reset period to
have any effect.

>> If you want to keep an io_service alive while there are no pending
>> operations, construct an io_service::work object from the io_service.
>>
>> That will keep io_service::run() running until all pending work is
>> finished (which you will indicate by destroying the work object.
>>
> I have tried and it works!

Hooray!

-- 
Lars Viklund | zao_at_[hidden]

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