Boost logo

Boost :

Subject: Re: [boost] [asio] How to initialize a strand
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2013-11-18 22:51:54


On 19/11/2013 16:30, Quoth Kyle Ketterer:
> I am just curious - how you can you initialize an strand via assignment? I
> am passing an strand by reference to an object so I have a reference
> variable declared:
>
> boost::asio::io_service::strand & strand_;
>
> In the constructor I would like to assign the reference variable an
> io_service (which has also been passed by reference).
>
> I would like to initialize strand_ in a way such as this ( I know it
> doesn't work)
>
> strand_ = boost::asio::io_service::strand(io_service_);

You can't do that. A strand must be given an io_service at construction
time.

If you want to accept a strand by reference in your constructor, that's
fine (as long as the lifetime of the strand exceeds that of your
object); it becomes the responsibility of whoever passes that strand in
to initialise it with the correct io_service. You can't change this
afterwards, because that would break the guarantees that the strand is
trying to make.

If you want to have a strand, but receive an io_service in your
constructor instead, then you'll have to declare your member variable as
a real object, not a reference. (And then verify that doing that still
meets the guarantees that you presumably want from using a strand in the
first place.)

(And for future reference, the boost-user mailing list is where this
sort of question should have been posted.)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk