microsoft PPL library: concurrent_vector push_back data only once

342 Views Asked by At

I have a Concurrency::concurrent_vector and want to push_back thread safe a new element only in case it not already exists (that's why I have to perform a search first). How can I accomplish this ? I need a concurrent collection that it's thread safe at iterator access(write,read same element) and also permits what I wrote above.

Question refinement: What would be the best container to use if it's mostly searched concurrently, if element is not found (more rare) an insert/push_back would be needed and in case search element is found (most of the cases) update to an element is needed (concurrent update of the same iterator)

From what is already available in VS2010 I saw concurrent_vector but that it is not safe at updating the same element and also it seems that I need an extra lock in case an element is not found and I need to add an element because of that. What do you think ? Is there something I can use to eliminate external locks (whole container locks)?

1

There are 1 best solutions below

0
On BEST ANSWER

For the case described probably the best would be to use (from Visual Studio) the concurrent_unordered_map container which is a hash based container that ensures concurrent iteration, iterator access (as long as the updates/reads to the same element value is synchronized!), push_back-s, element access based on key (operator []).

The container implementation is available as part of the Concurrency Runtime Sample Pack In case look-up by key is not necessary one could also use other concurrent containers: http://msdn.microsoft.com/en-us/library/dd504906.aspx