Moving an std::string into a boost::thread in C++03
Hi, I'm using boost 1.51 on multiple platforms and compilers without C++11. In my main thread, I have a very long, expensive to copy, std::string veryLongString, that I need to pass to a new thread for processing. After the new thread is created I have no more use for veryLongString, hence I'd like to *move *it into the boost::thread ctor. Obviously, if veryLongString was created as a shared_ptrstd::string then I could just *copy* the shared_ptr into the thread ctor, but it wasn't, so I'd need to copy it anyway. How can I [boost::]move() veryLongString into the boost::thread ctor (probably using via boost::bind)? Is this possible? Thanks, Adi
2012/10/4 Adi Shavit
Hi,
How can I [boost::]move() veryLongString into the boost::thread ctor (probably using via boost::bind)? Is this possible?
Hi, Adi sorry, bind() can only do as much also - either doing a copy of a std::string or using a reference to it. As you mentioned, you have to use shared_ptr<> to pass ownership or use boost::container::string which is movable (even in 03 compilers via boost.move emulation) instead of std::string. Cheers, Szymon
auto p = make_shared<string>();
p->swap( veryLongString );
And now you have a shared pointer with moved contents of veryLongString ;-)
Regards,
Kris
2012/10/4 Szymon Gatner
2012/10/4 Adi Shavit
: Hi,
How can I [boost::]move() veryLongString into the boost::thread ctor (probably using via boost::bind)? Is this possible?
Hi, Adi
sorry, bind() can only do as much also - either doing a copy of a std::string or using a reference to it.
As you mentioned, you have to use shared_ptr<> to pass ownership or use boost::container::string which is movable (even in 03 compilers via boost.move emulation) instead of std::string.
Cheers, Szymon _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Kris, Since I don't have C++11, I don't have auto but I tried this and it works. I guess I was hoping for a more (meta-)magical solution that allows bind to automatically create a move-copy or something... Thanks! Adi
auto p = make_shared<string>(); p->swap( veryLongString ); And now you have a shared pointer with moved contents of veryLongString ;-) Regards, Kris
participants (3)
-
Adi Shavit
-
Krzysztof Czainski
-
Szymon Gatner