是的boost ::进程间:: shared_ptr的线程(和进程安全的)? [英] Is boost::interprocess::shared_ptr threadsafe (and interprocess-safe)?

查看:417
本文介绍了是的boost ::进程间:: shared_ptr的线程(和进程安全的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想线程之间共享数据,并在最后一个用户用它做有它自动删除。这似乎工作,大部分时间,在提振使用的boost ::进程间:: shared_ptr的 :: fixed_managed_shared_memory 段:但并非总是如此。

I want to share data between threads, and have it automatically deleted when the last user is done with it. This seems to work, most of the time, using boost::interprocess::shared_ptr in a boost::fixed_managed_shared_memory segment: but not always.

那么,是的boost ::进程间:: shared_ptr的线程(和进程)-safe?

So, is boost::interprocess::shared_ptr thread (and interprocess) -safe?

如果我用我的共享内存在一个固定的地址(我pretty肯定这将是我在64位(当然,48位)地址空间还行),是有可能使用普通的的boost :: shared_ptr的(这是线程),而不是?

If I'm using my shared memory at a fixed address (I'm pretty certain this is going to be okay in my 64-bit (well, 48-bit) address space), is it possible to use a normal boost::shared_ptr (which are threadsafe) instead?

澄清:

我使用的指针类型是纯无效* ,(我的共享内存映射到一个固定的地址)。

The pointer type I use is plain void*, (my shared memory is mapped to a fixed address).

threadsafety的问题是关于参考计数 - 即,复制是否同时破坏共享指针在不同的过程同样的事情被允许/。 不会访问在不同的线程相同的共享指针,和不可以访问指针对象。

The question of threadsafety is about the reference count -- i.e., whether copying/destroying shared pointers to the same thing in different processes at the same time is permitted. Not access to the same shared pointer in different threads, and not access to the pointee.

推荐答案

的boost ::进程间使用引用计数:shared_ptr的正在使用中定义的原子计数实施升压/间/细节/ atomic.hpp 与主要由升压/间/ smart_ptr /细节/ sp_counted_base_atomic.hpp实现的引用计数逻辑。这样做的目的是为了有引用计数在一个线程(和进程)安全的方式进行处理。

The reference count used in boost::interprocess:shared_ptr is implemented using an atomic counter defined in boost/interprocess/detail/atomic.hpp with the refcount logic mainly implemented by boost/interprocess/smart_ptr/detail/sp_counted_base_atomic.hpp. The intent is to have the refcount be handled in a thread (and interprocess) safe manner.

的原子操作实施的不同而不同的特定目标平台(Windows使用的Win32互锁的API,一些平台使用各种内汇编,等等)。这可能有助于了解你的目标是什么平台。我想你可能正在运行到引用计数处理中的错误,但我不会指望它。

The atomic operation implementations differ depending on the specific target platform (Windows uses the Win32 Interlocked APIs, some platforms use various inline assembly, etc). It might be helpful to know what platform you're targeting. I suppose that you may be running into a bug in the refcount handling, though I wouldn't count on it.

我已经制约了以上回答对你就想具体阐述了区:

I've restricted the above answer to the area you wanted specifically addressed:

threadsafety的问题是关于参考计数 - 即,复制是否同时破坏共享指针在不同的过程同样的事情被允许/。不能访问在不同的线程相同的共享指针,而不是访问指针对象。

The question of threadsafety is about the reference count -- i.e., whether copying/destroying shared pointers to the same thing in different processes at the same time is permitted. Not access to the same shared pointer in different threads, and not access to the pointee.

不过,我想看看你所提到的上面或通过某种方式建立独立的boost ::进程间的项目可能引入的错误:shared_ptr的对象(其中不同的shared_ptr s请参阅使用不同的引用计数)相同的对象。这种情况可以很容易,如果您有一些code,它继续使用和/或通过原始对象的指针周围发生。

That said, I'd look at bugs introduced possibly by the items you mention above or by somehow creating 'independent' boost::interprocess:shared_ptr objects (where different shared_ptrs refer to the same object using different refcounts). This situation can be easy to have happen if you have some code that continues to use and/or pass around the raw object pointer.

这篇关于是的boost ::进程间:: shared_ptr的线程(和进程安全的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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