Boost logo

Boost Users :

Subject: Re: [Boost-users] storing a boost.thread in a boost shared ptr
From: Adam Romanek (a.romanek_at_[hidden])
Date: 2011-03-21 06:19:08


Hi,

you need to initialize boost::shared_ptr<boost::thread> with a pointer
to a boost::thread object, not with the object (instance) itself.

the preferred way of creating a new object and initializing a
boost::shared_ptr with it is to use boost::make_shared, like this:

boost::shared_ptr<boost::thread> t_ptr =
boost::make_shared<boost::thread>( xxx );

where xxx is the code you want to pass to boost::thread's constructor.
see the documentation of boost::make_shared for more details.

WBR,
Adam

On 03/21/2011 10:31 AM, Sean Farrow wrote:
> Hi:
> Yes, that works fine, the following replacement gives the error provided:
> boost::shared_ptr<boost::thread> t(boost::bind(&boost::asio::io_service::run, io.get()));
> Cheers
> Sean.
>
> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Kulti
> Sent: 21 March 2011 09:12
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] storing a boost.thread in a boost shared ptr
>
> On Mon, Mar 21, 2011 at 7:45 AM, Sean Farrow
> <sean.farrow_at_[hidden]> wrote:
>> Hi:
>>
>> I'm trying to store a boost.thread in a boost::fusion::vector, and need to
>> use a shared ptr, due to boost.thrad being non-copyable
>>
>> I'm using Microsoft visual c++ 2010.
>>
>>
>>
>> I get the following error:
>>
>> 1>d:\devexpress\mdrlite\mdrlite\mdrlite\midiwidget.cpp(21): error
>> C2664: 'boost::shared_ptr<T>::shared_ptr(boost::shared_ptr<T> &&)' : cannot
>> convert parameter 1 from 'boost::_bi::bind_t<R,F,L>' to
>> 'boost::shared_ptr<T> &&'
>>
>> with
>>
>> [
>>
>> T=boost::thread
>>
>> ]
>>
>> and
>>
>> [
>>
>> R=size_t,
>>
>> F=boost::_mfi::mf0<size_t,boost::asio::io_service>,
>>
>>
>> L=boost::_bi::list1<boost::_bi::value<boost::asio::io_service *>>
>>
>> ]
>>
>> and
>>
>> [
>>
>> T=boost::thread
>>
>> ]
>>
>> Reason: cannot convert from 'boost::_bi::bind_t<R,F,L>' to
>> 'boost::shared_ptr<T>'
>>
>> with
>>
>> [
>>
>> R=size_t,
>>
>> F=boost::_mfi::mf0<size_t,boost::asio::io_service>,
>>
>>
>> L=boost::_bi::list1<boost::_bi::value<boost::asio::io_service *>>
>>
>> ]
>>
>> and
>>
>> [
>>
>> T=boost::thread
>>
>> ]
>>
>> No user-defined-conversion operator available that can
>> perform this conversion, or the operator cannot be called
>>
>> My boost thread is defined as:
>>
>> boost::thread t(boost::bind(&boost::asio::io_service::run, io.get()));
>>
>> and io is defined as:
>>
>> boost::shared_ptr<boost::asio::io_service> io;
>>
>> I'm obviously overlooking something, I just carn't work out what?
>>
>> Any help appreciated.
>>
>> Regards
>>
>> Sean.
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>
> Hm... are you sure, that line 21 is boost::thread
> t(boost::bind(&boost::asio::io_service::run, io.get())); ? I'm trying
> to g++ -c following simple function and no errors occurred:
> void test()
> {
> boost::shared_ptr<boost::asio::io_service> io;
> boost::thread t(boost::bind(&boost::asio::io_service::run, io.get()));
> }
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


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