Threads, loops and CPU RRS feed

  • Question

  • I have few threads which uses ConcurrentQueue.

    and each one does :

    while (Q1.IsEmpty) ;

    where Q1 is a ConcurrentQueue.

    i have 8 ConcurrentQueue using loops, and i see on the server that this process even doing nothing

    consumes around 10-12% CPU constantly.

    is there a way to improve this?

    Monday, August 5, 2019 2:24 PM

All replies

  • If the purpose is to block until there is an item available, then you need to wrap your ConcurrentQueue with a BlockingCollection.  You can call BlockingCollection.Take and it will block until an item is available, with the thread asleep, using no CPU.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, August 5, 2019 5:46 PM
  • Did it really not occur to you to search for it?  Google is WAY faster than this forum, and the first hit for BlockingCollection describes its use.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, August 6, 2019 3:43 AM
  • Hi want 2 Learn, 

    Thank you for posting here.

    As Tim Roberts suggested, you can find more information in the following references.

    1. What are the differences between ConcurrentQueue and BlockingCollection in .Net?
    2. BlockingCollection<T> Class

    Besides, Queue + lock is a possible way to improve performance. You can refer the following reference.

    performance of ConcurrentQueue vs Queue + lock

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, August 6, 2019 6:30 AM