awaitable Task based queue

前端 未结 9 1189
礼貌的吻别
礼貌的吻别 2020-11-27 14:44

I\'m wondering if there exists an implementation/wrapper for ConcurrentQueue, similar to BlockingCollection where taking from the collection does not block, but is instead a

9条回答
  •  余生分开走
    2020-11-27 15:39

    I don't know of a lock-free solution, but you can take a look at the new Dataflow library, part of the Async CTP. A simple BufferBlock should suffice, e.g.:

    BufferBlock buffer = new BufferBlock();
    

    Production and consumption are most easily done via extension methods on the dataflow block types.

    Production is as simple as:

    buffer.Post(13);
    

    and consumption is async-ready:

    int item = await buffer.ReceiveAsync();
    

    I do recommend you use Dataflow if possible; making such a buffer both efficient and correct is more difficult than it first appears.

提交回复
热议问题