简体   繁体   English

增强智能指针和线程

[英]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. shared_ptr将用于共享数据。 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. shared_ptr用于共享所有权。

unique_ptr to transfer ownership from thread to thread unique_ptr在线程之间转移所有权

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 关于线程共享方式,shared_ptr可以做什么和不可以做什么,也确实有很好的例子: shared_ptr-线程安全
Just in case you want to do a bit more that just transfer the ownership 万一您想做更多的事情,只是转让所有权

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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