C# Threading and Queues

后端 未结 5 1513
不知归路
不知归路 2021-02-02 01:13

This isn\'t about the different methods I could or should be using to utilize the queues in the best manner, rather something I have seen happening that makes no sense to me.

5条回答
  •  难免孤独
    2021-02-02 01:31

    Here is what I think the problematic sequence is:

    1. (0 < queue.Count) evaluates to true, the queue is not empty.
    2. This thread gets preempted and another thread runs.
    3. The other thread removes an item from the queue, emptying it.
    4. This thread resumes execution, but is now within the if block, and attempts to dequeue an empty list.

    However, you say nothing else is dequeuing...

    Try outputting the count inside the if block. If you see the count jump numbers downwards, someone else is dequeuing.

提交回复
热议问题