Threadsafe FIFO Queue/Buffer

前端 未结 5 1408
无人及你
无人及你 2020-12-18 00:55

I need to implement a sort of task buffer. Basic requirements are:

  • Process tasks in a single background thread
  • Receive tasks from multiple threads
相关标签:
5条回答
  • 2020-12-18 01:20

    You could use Rx on .NET 3.5 for this. It might have never come out of RC, but I believe it is stable* and in use by many production systems. If you don't need Subject you might find primitives (like concurrent collections) for .NET 3.5 you can use that didn't ship with the .NET Framework until 4.0.

    Alternative to Rx (Reactive Extensions) for .net 3.5

    * - Nit picker's corner: Except for maybe advanced time windowing, which is out of scope, but buffers (by count and time), ordering, and schedulers are all stable.

    0 讨论(0)
  • 2020-12-18 01:25

    I suggest you take a look at TPL DataFlow. BufferBlock is what you're looking for, but it offers so much more.

    0 讨论(0)
  • 2020-12-18 01:33

    Look at my lightweight implementation of threadsafe FIFO queue, its a non-blocking synchronisation tool that uses threadpool - better than create own threads in most cases, and than using blocking sync tools as locks and mutexes. https://github.com/Gentlee/SerialQueue

    Usage:

    var queue = new SerialQueue();
    var result = await queue.Enqueue(() => /* code to synchronize */);
    
    0 讨论(0)
  • 2020-12-18 01:35

    You should think about ConcurrentQueue, which is FIFO, in fact. If not suitable, try some of its relatives in Thread-Safe Collections. By using these you can avoid some risks.

    0 讨论(0)
  • 2020-12-18 01:37

    You can actually handle this with the out-of-the-box BlockingCollection.

    It is designed to have 1 or more producers, and 1 or more consumers. In your case, you would have multiple producers and one consumer.

    When you receive a stop signal, have that signal handler

    • Signal producer threads to stop
    • Call CompleteAdding on the BlockingCollection instance

    The consumer thread will continue to run until all queued items are removed and processed, then it will encounter the condition that the BlockingCollection is complete. When the thread encounters that condition, it just exits.

    0 讨论(0)
提交回复
热议问题