创建一个非线程安全的shared_ptr [英] Creating a non-thread safe shared_ptr

查看:168
本文介绍了创建一个非线程安全的shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个MULT线程程序,但有一个UI组件,广泛使用的std :: shared_ptr的的管理要素。我可以保证只有一个线程将使用这些shared_ptrs。

I'm working on a mult-threaded program, but have a UI component that makes extensive use of std::shared_ptr to manage elements. I can guarantee that only one thread will ever use these shared_ptrs.

有没有办法来定义,它不会产生线程安全的引用计数的开销一个shared_ptr?

Is there a way to define a shared_ptr that doesn't incur the overhead of thread safe reference counting?

这可能是基于升压:: shared_ptr的或std :: shared_ptr的。

It could be based on boost::shared_ptr or std::shared_ptr.

编辑:谢谢你的答案提intrusive_ptr。我忘了提,我还需要weak_ptr的功能,这样的规则出来。

Thanks for answers mentioning intrusive_ptr. I neglected to mention that I also need weak_ptr functionality so that rules it out.

推荐答案

您可以使用的 intrusive_ptr ,因为它允许你提供自己的引用计数。如果引用计数是一个变量的简单递增/递减,你可能不会得到比这更好的性能。

you could use intrusive_ptr, as it allows you to provide your own reference counting. If that reference counting is a simple increment/decrement of a variable you probably won't get any better performance than that.

这篇关于创建一个非线程安全的shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆