开发者

An intrusive list of unique_ptrs?

I have a program that is highly multi-threaded and it contains an intrusive linked list of objects. I need to pass off the objects in this list to several threads, but only 1 thread will ever own the object at a time, mea开发者_运维问答ning that I don't need this object or the pointer to it be shared.

I wanted to create an intrusive list with a unique_ptr using boost, but from what I've read the unique_ptr would not be compatible with the Boost intrusive library as it does not have the right ownership semantics.

Per this the intrusive library requires it's elements (pointers) to have the same ownership semantics as a raw pointer. So unique_ptr or even shared_ptr would not qualify.

I wondered if anyone could give me some advice on how to best implement my intrusive list then so I can safely pass its elements through several threads and know that they are being MOVED to that thread and NOT shared amongst threads?


As far as I follow, for this to work you will need some kind of auto-unlink hooks.

Since the intrusive container does not own the objects it contains, you should not have any problem adding the raw pointers your unique_ptrs refer to, to the intrusive container.

If you need to be able to access the actual unique_ptr from the raw pointer in the intrusive list, something along the lines of enable_shared_from_this might help. (You'd need to combine your intrusive container with an intrusive unique_ptr.)


After thinking about it a bit it seems that there really isn't an intrusive unique_ptr variant out there, since the "intrusive" part for smart pointers usually is for the reference count, and unique_ptr like objects do not have a reference count.

Probably you would be best of using shared_ptr for this, since it already has enabled_shared_from_this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜