title: What are the common causes for high CPU usage?
body: <p><strong>Background:</strong></p>
  
  <p>In my application written in C++, I have created 3 threads:</p>
  
  <ul>
  <li>AnalysisThread (or Producer) : it reads an input file, parses it, and generates patterns, and enqueue them into <code>std::queue</code><sup>1</sup>.</li>
  <li>PatternIdRequestThread (or Consumer) : it deque patterns from the queue, and sends them, one by one, to database through a client (written in C++), which returns pattern <em>uid</em> which is then assigned to the corresponding pattern.</li>
  <li>ResultPersistenceThread : it does few more things, talks to database, and it works fine as expected, as far as CPU usage is concerned.</li>
  </ul>
  
  <p>First two threads take 60-80% of CPU usage, each takes 35% on average.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>I don't understand why some threads take high CPU usage.</p>
  
  <p>I analyse it as follows : if it is the OS who makes decisions like <a href="http://en.wikipedia.org/wiki/Context_switch" rel="noreferrer">context-switch</a>, <a href="http://en.wikipedia.org/wiki/Interrupt" rel="noreferrer">interrupt</a>, and <a href="http://en.wikipedia.org/wiki/Scheduling_%28computing%29" rel="noreferrer">scheduling</a> as to which thread should be given access to system resources, such as CPU time, then how come <em>some</em> threads in a process happen to use more CPU than the others? It looks like <em>some</em> threads forcefully takes CPU from the OS <em>at gunpoint</em>, or the OS has a real soft spot for some threads and so it is biased towards them from the very beginning, giving them all the resources it has. Why can't it be impartial and give them all equally?</p>
  
  <p>I know that it is naive. But I get confused more if I think along this line : the OS gives access to CPU to a thread, based on the amount of work to be done by the thread, but how does the OS compute or predict the amount of work <em>before</em> executing it completely?</p>
  
  <p>I wonder what are the causes for high CPU usage? How can we identify them? Is it possible to identify them just by looking at the code? What are the tools?</p>
  
  <p>I'm using Visual Studio 2010.</p>
  
  <p><sup>1. I've my doubt about <code>std::queue</code> as well. I know that standard containers aren't thread safe. But if exactly one thread enqueue items to queue, then is it safe if  exactly one thread deque items from it? I imagine it be like a pipe, on one side you insert data, on the other, you remove data, then why would it be unsafe if its done simultenously? But that is not the real question in this topic, however, you can add a note in your answer, addressing this.</sup></p>
  
  <p><strong>Updates:</strong></p>
  
  <p>After I realized that my consumer-thread was using busy-spin which I've fixed with <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx" rel="noreferrer">Sleep</a> for 3 seconds. This fix is temporary, and soon I will use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682400%28v=vs.85%29.aspx" rel="noreferrer">Event</a> instead. But even with <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx" rel="noreferrer">Sleep</a>, the CPU usage has dropped down to 30-40%, and occasionally it goes up to 50% which doesn't seem to be desirable from the usability point of view, as the system doesn't respond to other applications which the user is currently working with.</p>
  
  <p>Is there any way that I can still improve on the high CPU usage? As said earlier, the producer thread (which now uses most of the CPU cycles) reads a file, parses packets (of  some format) in it, and generates patterns out of them. If I use sleep, then the CPU usage would decrease but would it be a good idea? What are the common ways to solve it?</p>