网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++中怎么使用shared_ptr<T>共享所有权

这篇文章主要介绍“C++中怎么使用shared_ptr共享所有权”,在日常操作中,相信很多人在C++中怎么使用shared_ptr共享所有权问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++中怎么使用shared_ptr共享所有权”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联是一家网站建设、成都网站制作,提供网页设计,网站设计,网站制作,建网站,按需求定制设计,网站开发公司,从2013年创立是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。

F.27 使用shared_ptr共享所有权

Reason(原因)

Using std::shared_ptr is the standard way to represent shared ownership. That is, the last owner deletes the object.

使用std::shared_ptr是表现所有权共享的标准方式。使用这种方式时,最后一个所有者负责销毁对象。

Example(示例)

shared_ptr im { read_image(somewhere) };
std::thread t0 {shade, args0, top_left, im};std::thread t1 {shade, args1, top_right, im};std::thread t2 {shade, args2, bottom_left, im};std::thread t3 {shade, args3, bottom_right, im};
// detach threads// last thread to finish deletes the image

译者注:代码首先构造了一个Image对象并使用shared_ptr管理。然后将它们传递个4个线程共享这个对象。这段代码会启动四个线程,然后(应该)退出im的作用域。当所有线程结束时,最后一个结束的线程负责销毁Image对象。

Note(注意)

Prefer a unique_ptr over a shared_ptr if there is never more than one owner at a time.shared_ptr is for shared ownership.

Note that pervasive use of shared_ptr has a cost (atomic operations on the shared_ptr's reference count have a measurable aggregate cost).

如果同一时刻永远不会有多于一个所有者的话,unique_ptr比shared_ptr更合适。

Alternative(可选方式)

Have a single object own the shared object (e.g. a scoped object) and destroy that (preferably implicitly) when all users have completed.

让一个对象管理共享对象(例如范围对象)并且当所有用户都结束使用时(最好是隐式)销毁该(共享)对象。

Enforcement(实施建议)

(Not enforceable) This is a too complex pattern to reliably detect.

(不可执行)模式过于复杂以至于无法可靠地检查。

到此,关于“C++中怎么使用shared_ptr共享所有权”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


名称栏目:C++中怎么使用shared_ptr<T>共享所有权
新闻来源:http://bjjierui.cn/article/pieecs.html

其他资讯