While I usually use my own classes created before STL
existed. I decided to use these STL
objects and function in a standalone library that was already using STL
. I have a question about taking over a pointer (similar to MFC
having a .Detach()
method/function).
I have a worker thread with a queue of std::string
s (in std:deque
) where I want to, while guarded, assign the next item (front) of the queue to a local std::string
variable then .pop_front
the item out of the queue and releasing the guard (scoped operation).
I don't want to waste time and resources having it make a copy when all it needs to do is assign the pointer.
So my question is, does the STL
handle this so that the destruction of the two std::string
objects (referencing same pointer) will be okay (internal reference counting or nullifying the internals of the moved source)?
So basically:
void fun()
{
std::string my_string;
{
std::lock_guard<std::mutex> guard(my_mutex);
my_string = std::move(deque.front());
deque.pop_front()
}
// ... go on and using my_string below
}
TIA!!