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
on the main thread so I'd like to move it into the boost::thread
ctor.
The main thread, or the scope of veryLongString
may end before the new thread completes, so passing by reference (e.g. with boost::ref
) is not an option.
Obviously, if veryLongString
was created as a shared_ptr<std::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?
If the string is expensive to copy, pass something holding it but less expensive to copy. For example, you can use a shared_ptr<std::string>
. You can pass the shared pointer to a suitable wrapper which calls the function you actually want to get called (and probably taking the argument by reference or const
reference).
To get the string into a shared pointer you might need to move it there:
shared_ptr<std::string> ptr(new std::string);
ptr->swap(your_long_string);