C# Threading and Queues

后端 未结 5 1506
不知归路
不知归路 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:40

    Are the other areas that are "Enqueuing" data also using the same synchronized queue object? In order for the Queue.Synchronized to be thread-safe, all Enqueue and Dequeue operations must use the same synchronized queue object.

    From MSDN:

    To guarantee the thread safety of the Queue, all operations must be done through this wrapper only.

    Edited: If you are looping over many items that involve heavy computation or if you are using a long-term thread loop (communications, etc.), you should consider having a wait function such as System.Threading.Thread.Sleep, System.Threading.WaitHandle.WaitOne, System.Threading.WaitHandle.WaitAll, or System.Threading.WaitHandle.WaitAny in the loop, otherwise it might kill system performance.

提交回复
热议问题