开发者

Boost Smart Pointers and threading

开发者 https://www.devze.com 2023-01-18 03:01 出处:网络
If yo开发者_如何学运维u have to pass objects across threads which smart pointer type is best to use?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消