简体   繁体   中英

Async stack equivalent of TPL Dataflow BufferBlock<T>

BufferBlock<T> is a very nice async interface to a queue.

What I'm looking for is something like that but for a LIFO stack. I'm looking for equivalent functionality of BlockingCollection when passed a ConcurrentStack but instead of blocking a thread on Take and Add, I want to have TakeAsync and AddAsync . This is basically what BufferBlock<T> gives me but it's a FIFO queue, and I need a LIFO stack.

I am looking for a lock free version of an async stack. I don't care if it fits in nicely with dataflow or not, I was just using BufferBlock as an example. I'm not planning on using this in conjunction with dataflow.

TPL Dataflow of course doesn't support such functionality, otherwise it wouldn't have a "flow".

What you can do is use Stephen Cleary's AsyncCollection . It's an async wrapper around any IProducerConsumerCollection which in your case could be ConcurrentStack

var stack = new ConcurrentStack<int>();
var asyncStack = new AsyncCollection<int>(stack);

await asyncStack.AddAsync(4);
await asyncStack.AddAsync(6);

int top = await asyncStack.TakeAsync();

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