简体   繁体   中英

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 removing it, and later on if a certain condition is met, proceed with removing it. 删除它,稍后如果满足某个条件,则继续删除它。

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.

Are you sure ConcurrentQueue<T> doesn't suit your needs? It has a TryPeek method and a TryDequeue method that do exactly what you describe in the last paragraph.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM