1

i am building a network intrusion detection system using visual c++ and one of its component is a tcp session manager for the whole network. The tcp session data is stored in a concurrent vector so that it can be accessed from other threads. I found out there is no easy way to remove a session from the concurrent vector once the session is closed. So my QUESTION is what is the smartest method you know of for removing items from concurrent vectors.

Smart == fairly easy to program without too much performance hit Thankyou

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
yohannist
  • 4,166
  • 3
  • 35
  • 58
  • Have a look at [Erase-remove idiom](http://en.wikipedia.org/wiki/Erase-remove_idiom). Not sure if it is helpful for concurrent vectors but for vectors in general. – Mahesh Aug 06 '12 at 19:42
  • take a look at this stack overflow answer. http://stackoverflow.com/questions/347441/erasing-elements-from-a-vector – Richard Chambers Aug 06 '12 at 19:45
  • What is a "*concurrent vector*"? – Robᵩ Aug 06 '12 at 20:27
  • I'm pretty sure he means this [concurrent vector](http://msdn.microsoft.com/en-us/library/ee355343.aspx). – Karl Bielefeldt Aug 06 '12 at 21:48
  • @Rob and Karl, i am using concurrent_vector from intel's thread building library [www.threadingbuildingblocks.org](http://threadingbuildingblocks.org/) – yohannist Aug 07 '12 at 21:22

1 Answers1

1

I'd consider using a std::set instead of a vector here - especially if the number of items stored is large. I imagine you'll also want to perform lookups frequently.

Search and removal from the set is O(log(n)) complexity rather than O(n) for the std::vector - although the trivial insertion case in the is O(1) rather than O(log(n)) with the set.

You will also need mutex to protect all of these operations.

marko
  • 9,029
  • 4
  • 30
  • 46