title: Usage of spinlock functions
body: <p>How do I use these functions?
  I have declared lock globally.</p>
  
  <pre><code>pthread_spinlock_t lock;
  </code></pre>
  
  <p>Also the spinlock is initialized locally.</p>
  
  <pre><code>pthread_spin_init(&amp;lock, 1); // non-zero as pshared for IPC
  </code></pre>
  
  <p>But now I want to lock my critical integer and increment it.
  I have multiple processes running this in a loop:</p>
  
  <pre><code>while(0 != pthread_spin_trylock(&amp;lock));
  criticalInt += 1;
  pthread_spin_unlock(&amp;lock);
  </code></pre>
  
  <p>Why doesn't that work?
  Also, how is the following function used?</p>
  
  <pre><code>pthread_spin_lock(&amp;lock);
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre><code>for (i=0; i &lt; NUM_CHILDREN; i++) {
  
     pid[i] = fork();
     if (pid[i] == -1) { return EXIT_FAILURE; }
     if (pid[i] == 0) {
     while (criticalInt &lt; MAXCOUNT) {
        pthread_spin_lock(&amp;lock);
        criticalInt += 1;
        pthread_spin_unlock(&amp;lock);
        count++;
     }
     printf("Process %i counted %i\n", i, count);
  
  }
  </code></pre>
  
  <p>For a MAXCOUNT of 1000000 hat produces the following output:</p>
  
  <pre><code>    Process 3 counted 687858
      Process 0 counted 815657
      Process 1 counted 640191
      Process 2 counted 744340
  </code></pre>
  
  <p>Actually all together summed up should be 1000000. But they are not.</p>
  
  <p>I get similar results if I totally delete the lock.</p>