title: Pthreads in Mac OS X - Mutexes issue
body: <p>I'm trying to learn how to program parallel algorithms in C using POSIX threads. My environment is a Mac OS X 10.5.5 with gcc 4.</p>
  
  <p>Compiling:</p>
  
  <pre><code>gcc -Wall -D_REENTRANT -lpthread source.c -o test.o
  </code></pre>
  
  <p>So, my problem is, if I compile this in a Ubuntu 9.04 box, it runs smoothly in thread order, on Mac looks like mutexes doesn't work and the threads don't wait to get the shared information.</p>
  
  <p>Mac:</p>
  
  <pre><code>#1
  #0
  #2
  #5
  #3
  #4
  </code></pre>
  
  <p>ubuntu</p>
  
  <pre><code>#0
  #1
  #2
  #3
  #4
  #5
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Follow below the source code:</p>
  
  <pre><code>#include &lt;ctype.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;pthread.h&gt;
  
  #define NUM_THREADS 6
  pthread_mutex_t mutexsum;
  pthread_t threads[NUM_THREADS];
  long Sum;
  
  void *SumThreads(void *threadid){
      int tmp;
      int i,x[10],y[10];
  
      // Para cada x e y do vetor, jogamos o valor de i, só para meio didáticos
      for (i=0; i&lt;10 ; i++){
          x[i] = i;
          y[i] = i;
      }
  
  
      tmp = Sum;
       for (i=0; i&lt;10 ; i++){
           tmp += (x[i] * y[i]);
       }
  
         pthread_mutex_lock (&amp;mutexsum);
         Sum += tmp;
         printf("Im thread #%ld sum until now is: %ld\n",threadid,Sum);
         pthread_mutex_unlock (&amp;mutexsum);
         return 0;
  }
  
  
  int main(int argc, char *argv[]){
      int i;
      Sum = 0;
  
      pthread_mutex_init(&amp;mutexsum, NULL);
  
      for(i=0; i&lt;NUM_THREADS; i++){
          pthread_create(&amp;threads[i], NULL, SumThreads, (void *)i);
      }
  
      pthread_exit(NULL);
  }
  </code></pre>