0

I have:

  1. class X
  2. boost::unordered_map of X
  3. List of X - used to timeout element according to timestamp

  4. I need to insert and remove element from list with performance. I though using intrusive list, so an element can remove itself from the list without going through all the list.

  5. I want to wrap X with shared_ptr so it won't leak...

I discovered that shared_ptr is not compatible with intruive ptr.
1. Do you have another proposition?

Using multi_index of shared_ptr with 2 indexes(hashed and ordered) could be the best solution, but i can't use it.

2.Is oredered index in multi index efficient for removing element?

yaron
  • 439
  • 6
  • 16
  • Is the list ordered by insertion time or do the timeouts come in some other order? I.e. is it like every element dies after 5 seconds, or every element has a configurable timeout? – John Zwinck Jul 09 '14 at 10:01
  • Since you'd be fine with an intrusive list, could the `X` object just store an iterator into the list? – Angew is no longer proud of SO Jul 09 '14 at 10:06
  • @JohnZwinck The list is ordered at insertion time. – yaron Jul 09 '14 at 10:38
  • @Angew Do you mean to store an iterator to a regular list element? Is this legal? – yaron Jul 09 '14 at 10:40
  • Use `make_shared` and move the `shared_ptr`, then it's (almost) equivalent to a raw pointer. Will work with any kind of list or container. Copying a `shared_ptr` is expensive since it needs an atomic modification of the refcount. Moving doesn't change the number of references, so that's a simple binary copy of a two-pointer-sized structure. – Damon Jul 09 '14 at 10:57
  • 1
    @davidbobo Why should storing an iterator be illegal? `std::list` iterators are never invalidated as long as the element is actually in the list. – Angew is no longer proud of SO Jul 09 '14 at 11:00

0 Answers0