Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safe collection that supports removing items

I cannot seem to find a .NET thread safe / concurrent collect that supports a simple Remove() function where I can either remove a specific item or pass in a predicate to remove items based on that. I have tried:

BlockingCollection<T>
ConcurrentQueue<T>
ConcurrentStack<T>
ConcurrentBag<T>

Does anyone know of a collection that supports this behavior, or do I have to create my own?

I want to be able to grab the next item from a thread-safe queue without removing it, and later on if a certain condition is met, proceed with removing it.

like image 584
Sean Thoman Avatar asked Dec 09 '22 02:12

Sean Thoman


1 Answers

Have you tried ConcurrentDictionary? It has a TryRemove method, so if you think of the key as a predicate, then you will be removing the correct item.

like image 69
Kiril Avatar answered Dec 23 '22 18:12

Kiril