If yo开发者_如何学运维u have to pass objects across threads which smart pointer type is best to use?
Assuming the object being passed is thread safe.
A shared_ptr
would work for sharing data. Its counter is atomic, so you won't run into problems there, and when the last thread is done it goes away.
shared_ptr
for shared ownership.
unique_ptr
to transfer ownership from thread to thread
Just a little hint:
There are also really good examples about what you can and what you can't do with shared_ptr in a thread safe manner: shared_ptr - thread safety
Just in case you want to do a bit more that just transfer the ownership
精彩评论