title: Cat unix command multithread implementation
body: <p>Hi im trying to implement faster cat than the one provided.</p>
  
  <p>My current implementation looks like this: </p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #define BUF_SIZE 1024*1024*1024
  
  char buffer[BUF_SIZE];
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
  pthread_cond_t cond_var2 = PTHREAD_COND_INITIALIZER;
  int readed = 0;
  /*
      Read characters from standard input and saves them to buffer
  */
  void *consumer(void *data) {
      int r;
      while(1) {
          //---------CRITICAL CODE--------------
          //------------REGION------------------
          pthread_mutex_lock(&amp;mutex);
          if (readed &gt; 0)
          {
              pthread_cond_wait(&amp;cond_var2, &amp;mutex);
          }
          r = read(0, buffer, BUF_SIZE);
          readed = r;
  
          pthread_cond_signal(&amp;cond_var);
          pthread_mutex_unlock(&amp;mutex);
          //------------------------------------
  
          if (r == -1){
              printf("Error reading\n");
          }  
          else if (r == 0) {
              pthread_exit(NULL);
          }
      }
  }
  
  /*
      Print chars readed by consumer from standard input to standard output
  */
  void *out_producer(void *data) {
      int w;
      while(1){    
          //---------CRITICAL CODE--------------
          //-------------REGION-----------------
          pthread_mutex_lock(&amp;mutex);
          if (readed == 0)
          {
              pthread_cond_wait(&amp;cond_var, &amp;mutex);
          }
          w = write(1, buffer, readed); 
          readed = 0;
          pthread_cond_signal(&amp;cond_var2);
          pthread_mutex_unlock(&amp;mutex);
          //------------------------------------ 
  
          if (w == -1){
              printf("Error writing\n");
          } 
          else if (w == 0) {
              pthread_exit(NULL);
          }
      }
  }
  </code></pre>
  
  <p>What would you suggest to make it faster? 
  Any ideas? 
  I was thinking about the BUF_SIZE, what would you think would be optimal size of buffer? </p>
  
  <p>Main just makes the threads:</p>
  
  <pre><code>int main() {
      //  Program RETURN value
      int return_value = 0;
  
      //  in - INPUT thread
      //  out - OUTPUT thread
      pthread_t in, out;
  
      //  Creating in thread - should read from standard input (0)
      return_value = pthread_create(&amp;in , NULL, consumer, NULL);
      if (return_value != 0) {
          printf("Error creating input thread exiting with code error: %d\n", return_value);
          return return_value;
      }
  
      //  Creating out thread - should write to standard output (1)
      return_value = pthread_create(&amp;out, NULL, out_producer, NULL);
      if (return_value != 0) {
          printf("Error creating output thread exiting with code error: %d\n", return_value);
          return return_value;
      }
  
      return_value = pthread_join(in, NULL);
      return_value = pthread_join(out, NULL);
  
      return return_value;
  }
  </code></pre>