0

I have std::set having large number unique objects as its elements.

In the main thread of program:

  1. I take some objects from the set
  2. Assign data to be processed to each of them
  3. Remove those objects from set
  4. And finally pass the objects to threads in threadpool for processing
  5. Once those threads finish processing objects, they adds them back to the set. (So that in the next iteration, main thread can again assigns next batch of data to those objects for processing)

This arrangement works perfect. But if I encounter error while adding back object to the set (for example, std::set.insert() throws bad_alloc) then it all goes on toss. If I ignore that error and proceed, then there is no way for the object to get back in the processing set and it remains out of the program flow forever causing memory leaks.

To address this issue I tried to not to remove object from set. Instead, have a member flag that indicates the object is 'being processed'. But in that case the problem is, main thread encounters 'being processed' objects again and again while iterating through all elements of set. And it badly hampers performance (Number of objects in set are quite large).

What are better alternatives here?

  1. Can std::list be used instead of std::set? List will not have bad_alloc problem while adding back element, as it just needs to assign pointers while adding element to list. But how can we make list elements unique? If at all we achieve it, will it be efficient as std::set?

  2. Instead of removing and adding back elements to the std::set, is there any way to move element to the start or end of the set? So that unprocessed objects and processed will accumulate together towards start and end of the set.

  3. Any other solution please?

Atul
  • 3,778
  • 5
  • 47
  • 87
  • 1
    *"I tried to not to remove object from set..."* - can't you use an iterator into the set to track which element will be next to be processed? Why were you "encountering [the] same objects again and again"? – Tony Delroy Oct 29 '14 at 06:13
  • I mean to say "encountering 'still being processed elements' again and again" while iterating through the set. – Atul Oct 29 '14 at 06:15
  • @Tony D: To avoid confusion and to make it clearer, I modified the question please have a look. Thanks. – Atul Oct 29 '14 at 06:31
  • 1
    I'm still confused... presumably you don't have more threads than cores, and one object being processed on each, so there's a relatively small number of concurrently "being processed" objects, while you've said the set is "quite large" - if you iterate you'd seem unlikely to hit many such objects. Anyway, `list`s don't guarantee uniqueness, but you can certainly have a separate list of pointers to objects in the set. If you're getting `bad_alloc` your program's unlikely to be reliable though - you need to rethink your memory usage. – Tony Delroy Oct 29 '14 at 06:53
  • It's little more complicated. They are being queued up. I didn't mentioned that part to make question easy to understand. But yes please assume number of "being processed" objects gets increased eventually in the set. – Atul Oct 29 '14 at 07:38
  • Is your `set` protected by `mutex` ? – Jarod42 Oct 29 '14 at 08:25
  • Yes, thread safety is taken care. Only problem is when it runs out of memory. In normal scenario it shouldn't. But this has came up during memory stress test. – Atul Oct 29 '14 at 08:31

0 Answers0