Profiling C++ threads with clock()
<p>I am trying to measure how gcc threads perform on my system. I've written some very simple measurement code which is something like this...</p>

<pre><code>start = clock();
for(int i=0; i &lt; thread_iters; i++) {
  pthread_mutex_lock(dataMutex);
  data++;
  pthread_mutex_unlock(dataMutex);
}
end = clock();
</code></pre>

<p>I do the usual subtract and div by CLOCKS_PER_SEC to get an elapsed time of about 2 seconds for 100000000 iterations. I then change the profiling code slightly so I am measuring the individual time for each mutex_lock/unlock call.</p>

<pre><code>for(int i=0; i &lt; thread_iters; i++) {
  start1 = clock();
  pthread_mutex_lock(dataMutex);
  end1 = clock();
  lock_time+=(end1-start1);

  data++;

  start2 = clock();
  pthread_mutex_unlock(dataMutex);
  end2 = clock();
  unlock_time+=(end2-start2)
}
</code></pre>

<p>The times I get for the same number of iterations are
lock: ~27 seconds
unlock: ~27 seconds</p>

<p>I get why the total time for the program increases, more timer calls in the loop. But the time for the system calls should still add up to less than 2 seconds. Can someone help me figure out where I went wrong? Thanks!</p>
