title: Odd/Incorrect sem_getvalue Semaphore Behavior on OS X
body: <p>I have some very basic semaphore code that works great on Linux, but cannot for the life of me get it to run properly on OS X... It returns the oddest of results...</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;semaphore.h&gt;
  
  int main()
  {
      sem_t* test;
      test = sem_open("test", O_CREAT, 0, 1);
  
      int value;
      sem_getvalue(test, &amp;value);
      printf("Semaphore initialized to %d\n", value);
  }
  </code></pre>
  
  <p>Compiling this on OS X with g++ returns the following output:</p>
  
  <pre><code>iQudsi:Desktop mqudsi$ g++ test.cpp
  iQudsi:Desktop mqudsi$ ./a.out 
  Semaphore initialized to -1881139893
  </code></pre>
  
  <p>Whereas on Ubuntu, I get the decidedly more-sane result:</p>
  
  <pre><code>iQudsi: Desktop mqudsi$ g++ test.cpp -lrt
  iQudsi:Desktop mqudsi$ ./a.out 
  Semaphore initialized to 1
  </code></pre>
  
  <p>I've been at this for 3 hours straight, and cannot figure out why OS X is returning such bizarre results...</p>
  
  <p>I've tried using file paths as the semaphore name, it didn't make a difference.</p>
  
  <p>I'd appreciate any help I could get.</p>