title: How to programmatically get the CPU utilization by the parent thread and sibling threads, from a worker thread?
body: <p>My multi-threaded application (c++) uses pthreads. The application automatically generates threads and re-uses them on-demand, and allows a thread to be cancelled when it's been idling for too long.</p>
  
  <p>I have put in a "special thread" to capture statistical data for looking at application's performance, when subjected to different scenarios. </p>
  
  <p>I can't wait for the process to exit, to know how much CPU was used.</p>
  
  <p>Tried using this from the special thread, but the results did not seem to match the output of ps &amp; top:</p>
  
  <pre><code>struct rusage ru;
      double utime, stime, ttime ;
  
  if (getrusage(RUSAGE_SELF, &amp;ru) == -1)
      return;
  
  utime = ru.ru_utime.tv_sec + ( ru.ru_utime.tv_usec / 1000000 ); // Total CPU time used in user space
  stime = ru.ru_stime.tv_sec + ( ru.ru_stime.tv_usec / 1000000 ); // Total CPU time used in kernel space
  ttime = utime + stime; // Total CPU Utilization
  </code></pre>
  
  <p>Can you good hacks, tell me if there's a better way to do it, or the above is the correct method, and I am actually missing something else?</p>
  
  <p>Thanks in advance.</p>