title: C - Output Thread shall update when other threads modify values
body: <p>I have a program that does some calculation by using multi threading and adds the result to a global variable. There is also a counter of how many threads have already finished and how many are still running. The threads are detached. In addition to these threads there is an output thread that recognizes if another thread changed one of these global variables. If so then it shall print them. The output thread also waits passive.</p>
  
  <p>How do I make the output thread wait for changes in the global values? I could do a while loop until all threads finished and watch for changing values but that is not really what I want. I want the output thread to wait passively.</p>
  
  <pre><code>sem_t sem;
  int val1 = 0;
  int val2 = 0;
  int active = 0;
  int finish = 0;
  
  void* calc(void* arg){
      sem_wait(&amp;sem);
      calculate(arg);
      ++finish;
      sem_post(&amp;sem);
  
      return NULL;
  }
  
  void* out(void* arg){
  
      //what to do here?
  
      printf();
      return NULL;
  }
  
  int main(int argc, char* argv[]){
      pthread_t calcThread;
      int somevalue;
      sem_init(&amp;sem, 0, argv[1]); // max amount of threads running simultaniously
      for(int i = 2; i != argc; i++){
          somevalue = atoi(argv[i]);
          pthread_create(&amp;calcThread, NULL, calc, &amp;somevalue))
          pthread_detach(calcThread);
      }
  }
  </code></pre>