简体   繁体   中英

Boost Smart Pointers and threading

If you 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM