What are the common causes for high CPU usage?

前端 未结 8 672
半阙折子戏
半阙折子戏 2020-12-13 08:19

Background:

In my application written in C++, I have created 3 threads:

  • AnalysisThread (or Producer) : it reads an input file, parses
相关标签:
8条回答
  • 2020-12-13 08:40
    1. use asynchronous (file and socket) IO to reduce useless CPU waiting time.
    2. use vertical threading model to reduce context switch if possible
    3. use lock-less data structure
    4. use a profiling tool, such as VTune, to figure out the hot spot and make optimization
    0 讨论(0)
  • 2020-12-13 08:43

    Edit: Ok, since you are using busy spin to block on the queue, this is most likely the cause for high CPU usage. The OS is under the impression that your threads are doing useful work when they are actually not, so they get full CPU time. There was interesting discussion here: Which one is better for performance to check another threads boolean in java

    I advise you to either switch to events or other blocking mechanisms or use some synchronized queue instead and see how it goes.

    Also, that reasoning about the queue being thread-safe "because only two threads are using it" is very dangerous.

    Assuming the queue is implemented as a linked list, imagine what can happen if it has only one or two elements remaining. Since you have no way of controlling the relative speeds of the producer and the consumer, this may well be the case and so you're in big trouble.

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