Processing a queue of items asynchronously in C#

后端 未结 3 938
执念已碎
执念已碎 2021-02-06 16:36

I am trying to create a system that processes a queue of work. The system has the following specifications:

  1. The system has two components, a work assigner and a wo
3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 17:37

    I don't know how long your tasks will be running, but it seems that the best thing to do would be to use ThreadPool. Furthermore, I would use, and actually have used, only one central queue - that alone will remove some complexity. I have one Thread that handles the queue and does an action on the item in your case it would be to queue a task.

    As for making the queue threadsafe, there is a ConcurrentQueue in System.Collections.Concurrent for that very purpose (msdn, benchmark vs locking queue).

    Now, throw in a BlockingCollection (msdn) and you have all you need.

            BlockingCollection sendQueue = new BlockingCollection(new ConcurrentQueue());
            while (true)
            {
                var packet = sendQueue.Take(); //this blocks if there are no items in the queue.
                ThreadPool.QueueUserWorkItem(state =>
                {
                   var data = (Packet)state;
                   //do whatever you have to do
                }, packet );
            }
    

    and somewhere there is something that sendQueue.Add(packet);

    To sum up,

    1. One queue for all "workers"
    2. One thread that dequeues from the queue and passes it to the ThreadPool.

    I think that's it.

    ps: if you have to control the amount of threads, use "Smart Thread Pool" as suggested by josh3736

提交回复
热议问题