title: Notorious behaviour of pthread program
body: <p>I'am novice in programming and just started using pthread in c language.I was curious about the degree of performance improvement by multi-threading. To test this I wrote a simple program to calculates the sum of n digits(honestly, took it from youtube video). I gave it some real big numbers to get some values of execution time.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;pthread.h&gt;
  long long sum=0,pod=1;
  void* sum_run(void* arg)
  {
      long long *var_ptr=(long long *)arg;
      long long i,var=*var_ptr;
      for(i=0;i&lt;=var;i++)
      {
          sum+=i;
      }
      pthread_exit(0);
  }
  
  void* sum_run2(void* arg)
  {
      long long *var_ptr2=(long long *)arg;
      long long j,var2=*var_ptr2;
      for(j=0;j&lt;=var2;j++)
      {
          pod+=j;
      }
      pthread_exit(0);
  }
  
  int main(void)
  {
      printf("wait getting it...\n");
      long long val=999999999,val2=899999999;
      pthread_t tid[1];
      pthread_create(&amp;tid[0],NULL,sum_run,&amp;val);
      pthread_create(&amp;tid[1],NULL,sum_run2,&amp;val2);
      pthread_join(tid[0],NULL);
      pthread_join(tid[1],NULL);
      printf("sum1 is %lld sum2 is %lld",sum,pod);
  }
  </code></pre>
  
  <p>O yeah, by mistake I initiated the second long long variable pod to 1 which gave me false result (i.e. 1 more than the desired). So , I corrected my mistake and made pod=0 and here came the PROBLEM after changing it my program's execution time increased to more than twice even larger than the program which does the same task without using pthread. I can't think of what's happening inside. Please help the program.</p>
  
  <p>pod=1   exec.time=~2.8secs</p>
  
  <p>pod=0   exec.time=~11.4secs</p>
  
  <p>when sum=1  pod=1  exec.time bounces to <strong>~25.4secs</strong></p>
  
  <p>Why is it shifting due to changing values?</p>
  
  <p>Also, I found out if one variable is 0 and other's not then their addresses are not continuous.</p>
  
  <p>Using Devcpp's gcc4.9.2 with -pthread switch</p>