title: Is it possible to have an infinite loop in pthread?
body: <p>I am doing a project with pthreads and I can not go further. I have 3 threads, one thread shall read some sensor values via I2C (SMBUS), one shall calculate something by using the data porvided by the first thread, and finally last thread shall print the data on screen. A mutex shall be responsible for the synchronisation between threads. How can I use infinite <code>while</code> loop in threads, since my threads don't just "do the job and finish"? They shall continue forever (or untill Ctrl+C key is pressed).</p>
  
  <p>And where should I put the <code>pthread_join</code> functions?</p>
  
  <p>My first approach is below but it is not working properly:</p>
  
  <p>Thanks for your help.</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt; 
  #include &lt;time.h&gt; 
  #include &lt;linux/i2c-dev.h&gt;
  #include &lt;fcntl.h&gt;
  
  typedef struct vector {
      double x;
      double y;
      double z;
  } vector_t;
  
  typedef struct all_vectors {
      vector_t *gyro;
      vector_t *accel;
      vector_t *magnet;
  } vectors_t;
  
  vectors_t *vectors;
  
  pthread_t th1, th2, th3;
  pthread_mutex_t mutex;
  
  int main()
  {
      initSensors(); //defined in another .c file
  
      pthread_mutex_init(&amp;mutex, NULL);
      pthread_create(&amp;th1, NULL, (void *) &amp;readSensors, (void *) vectors);
      pthread_create(&amp;th2, NULL, (void *) &amp;calculateAngle, NULL);
      pthread_create(&amp;th3, NULL, (void *) &amp;printData, NULL);
  
  
      while(1)
      {
        sleep(1);
      }
  
      /* The program never reaches the following codes ? */ 
      pthread_mutex_destroy(&amp;mutex);
      pthread_exit(NULL);
  }
  
  
  void readSensors(void *vectors)
  {   
      vectors_t *vecs = (vectors_t *)vectors;
  
      while(1)
      {   
              pthread_mutex_trylock(&amp;mutex);
              readGyro(vecs-&gt;gyro); //defined in another .c file
              readAccel(vecs-&gt;accel); //defined in another .c file
              readMagnet(vecs-&gt;magnet); //defined in another .c file
              pthread_mutex_unlock(&amp;mutex);
      }
  
      pthread_exit(NULL);
  }
  
  
  void calculateAngle()
  {
      while(1)
      {
              pthread_mutex_trylock(&amp;mutex);
  
              doSomeCalculation(vectors); //defined in another .c file
  
              pthread_mutex_unlock(&amp;mutex);
      }
      pthread_exit(NULL);
  }
  
  void printData()
  {
  
      while(1)
      {
          pthread_mutex_trylock(&amp;mutex);
  
          printf("%lf, %lf, %lf, lf, %lf, %lf, lf, %lf, %lf", \
                 vectors-&gt;accel-&gt;x, vectors-&gt;accel-&gt;y, vectors-&gt;accel-&gt;z, \
                 vectors-&gt;gyro-&gt;x, vectors-&gt;gyro-&gt;y, vectors-&gt;gyro-&gt;z, \
                 vectors-&gt;magnet-&gt;x, vectors-&gt;magnet-&gt;y, vectors-&gt;magnet-&gt;z );
          pthread_mutex_unlock(&amp;mutex);
  
          fflush(stdout); 
      }
  
      pthread_exit(NULL);
  }
  </code></pre>