I have a situation where some object has to be passed as an argument of a thread callback function. Object is created dynamically and after it is passed to the thread, object is not needed/used in that context (in a method which starts a thread) any more. Thread function is now the only context which should own the object.
Assuming I want to use some Boost smart pointer (instead of passing to the thread a raw pointer), which one would be the most appropriate here? What is the best practice in this case?
What I actually need is std::auto_ptr with its move semantics through copy constructor. I believe that this smart pointer would perfectly fit here but it is deprecated for well-known reasons (and I cannot rely on tr1 and C++11 pointers; must (and want to) use Boost only as this code is shared between projects that must compile both in Visual Studio 2008 and 2010).
boost::shared_ptr is an option – I could pass it by value but think it would be overkilling. Is there any chance of emulating move semantics (in a safe way) with boost::scoped_ptr? I don’t need reference counting here as am not sharing object between two contexts, I just want to transfer the ownership over object from one context to another.
You could use boost::interprocess::unique_ptr, or write your own
unique_ptrusing Boost.Move.boost::interprocess::unique_ptruses Boost.Move in its implementation, and Boost.Move emulates C++11 move semantics C++03.