Strange issue using sem_t + pthread_create
<p>Strange behaviour when passing argument sem_t to constructor A. Expected output was <code>5555</code> but i got <code>5055</code>. Please point out if there are design problems too.</p>

<pre><code>  1 #include &lt;iostream&gt;
  2 #include &lt;pthread.h&gt;
  3 #include &lt;semaphore.h&gt;
  4 using namespace std;
  5 
  6 class A {
  7   public:
  8     pthread_t thr_id;
  9     int&amp; k;
 10 
 11     A(sem_t&amp; sem, int k) : k(k){}
 12     A(int k) : k(k){}
 13 
 14     void start(){
 15       cout &lt;&lt; k;
 16       pthread_create(&amp;thr_id, NULL, foo2, NULL);
 17       cout &lt;&lt; k;
 18     }
 19     void join(){
 20       pthread_join(thr_id, NULL);
 21     }
 22     static void* foo2(void* i){}
 23 };
 24 
 25 int main() {
 26   sem_t sem;
 27   A* ac1 = new A(sem, 5);
 28   ac1-&gt;start();
 29   ac1-&gt;join();
 30   A* ac2 = new A(5);
 31   ac2-&gt;start();
 32   ac2-&gt;join();
 33  return 0;
 34 }
</code></pre>
