[asio] How to initialize a strand

Hello, 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_); Any ideas? Thanks

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.)

Thanks for clearing that up. I presume the better way to go out about I
want to do is to just pass the strand as a reference then I can use the
get_io_service method to retrieve a reference to the io_service.
On Mon, Nov 18, 2013 at 10:51 PM, Gavin Lambert
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.)
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/ mailman/listinfo.cgi/boost
--
Kyle Ketterer
participants (2)
-
Gavin Lambert
-
Kyle Ketterer