title: Pthread and C language
body: <p>Here is my code:</p>
  
  <pre><code>static long x = 0;
  void * thread_func(void *arg){
  while(1){
      printf("Thread 2 says %ld\n",++x);
      sleep(1);
  }
  }
  int main(){
  pthread_t tid;
  pthread_create(&amp;tid,NULL,thread_func,NULL);
  while(x &lt; 10)   {
      printf("Thread 1 says %ld\n",++x);
      sleep(2);
  }
  return 0;
  }
  </code></pre>
  
  <p>Now what shall be the output, the book says it will be:</p>
  
  <pre><code>OUTPUT
  Thread 1 says 1
  Thread 2 says 2
  Thread 2 says 3
  Thread 1 says 4
  Thread 2 says 5
  Thread 2 says 6
  Thread 1 says 7
  Thread 2 says 8
  Thread 2 says 9
  Thread 1 says 10
  Thread 2 says 11
  Thread 2 says 12
  </code></pre>
  
  <p>Now shall not the first one should be Thread 2 says 1, since the <code>pthread_create()</code> is before the while loop in main.
  Also how does sleep work here?
  Like what exactly is a thread sleeping for, and what process executes when the executing thread sleeps.?</p>
  
  <p>i.e  sleep(1) and sleep(2)  what does 1 and 2 signify?</p>