title: pthread and semaphore not working for me in osx maverick 10.9
body: <p>I have the following simple program involving pthread and semaphore. I am in osx Maverck 10.9. I use a makefile to compile the program (rather than xcode). I use c++11.</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;cassert&gt;
  #include &lt;iostream&gt;
  
  #define ASSERT(a) if(!(a)) abort
  
  using namespace std;
  
  sem_t countMutex;
  int myCount=0;
  
  void *doThread(void *data) {
      int *pi = reinterpret_cast&lt;int *&gt;(data);
      sem_wait(&amp;countMutex);
      for(int i =0 ;i &lt; 100; ++i) {
          myCount += 1;
      }
      sem_post(&amp;countMutex);
      pthread_exit( NULL );
  }
  
  void LaunchThread() {
      const int kNumThreads = 10;
      pthread_t tids[kNumThreads];
      int threadData[kNumThreads];
      pthread_attr_t attr;
      pthread_t tid;
      int retVal=0;
      retVal = pthread_attr_init(&amp;attr);
      ASSERT(retVal == 0);
      retVal = pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE );
      ASSERT(retVal == 0);
      sem_init(&amp;countMutex, 0, 1);
      myCount = 0;
      for(int i=0; i &lt; kNumThreads; ++i) {
          threadData[i] = i;
          retVal = pthread_create( &amp;tids[i], &amp;attr, &amp;doThread, &amp;threadData[i]);
          if(retVal != 0) {
              cerr &lt;&lt; "cannot create thread" &lt;&lt; endl;
              return;
          }
      }
  
      retVal = pthread_attr_destroy(&amp;attr);
      ASSERT(retVal == 0);
      void *status = NULL;
      for(int i=0; i &lt; kNumThreads; ++i) {
          retVal = pthread_join( tids[i], &amp;status);
          if(retVal != 0) {
              cerr &lt;&lt; "cannot join ghread " &lt;&lt; i &lt;&lt; ", " &lt;&lt; tids[i] &lt;&lt; endl;
              return;
          }
          cout &lt;&lt; "completed thread " &lt;&lt; i &lt;&lt; ", " &lt;&lt; tids[i] &lt;&lt; endl;
      }
      cout &lt;&lt; "value of myCount: " &lt;&lt;  myCount &lt;&lt; endl;
      sem_destroy(&amp;countMutex);
      //sem_unlink(&amp;countMutex);
      pthread_exit( NULL );
  }
  int main( int argc, char **argv) {
      LaunchThread();
      return 0;
  }
  </code></pre>
  
  <p>The makefile for compiling this is</p>
  
  <pre><code>CXX=clang++
  CXXFLAGS=-g -Wall -Wno-deprecated -std=c++11 -pthread  -D DEBUG -g3 $(INCLUDES)
  LDFLAGS=$(LIBS)
  
  OBJS=main.o
  PROG=test
  
  all: $(PROG)
  
  
  $(PROG): $(OBJS)
  $(CXX) -v -o $(PROG) main.o $(LDFLAGS)
  
  %.o: %.cpp
  $(CXX) -c $(CXXFLAGS) $&lt;
  
  clean:
  rm $(OBJS); rm test
  </code></pre>
  
  <p>The program ought to have reported a value of 1000 for <code>myCount</code>. But is inconsistent, on multiple runs.</p>
  
  <p>Eg:</p>
  
  <pre><code>completed thread 0, 0x107dca000
  completed thread 1, 0x107e4d000
  completed thread 2, 0x107ed0000
  completed thread 3, 0x107f53000
  completed thread 4, 0x107fd6000
  completed thread 5, 0x108059000
  completed thread 6, 0x1080dc000
  completed thread 7, 0x10815f000
  completed thread 8, 0x1081e2000
  completed thread 9, 0x108265000
  value of myCount: 900
  </code></pre>