0

I wrote the following simple code:

#include <pthread.h> 
#include <stdio.h>
#include <stdlib.h>

void* printThreadFunction()
{
    puts("***got here*****");   
}


int main (int argc, char *argv[])
{
     pthread_t printThread;
     if (pthread_create(&printThread, NULL, printThreadFunction, NULL) != 0) {
             perror("pthread_create");
             exit(1);
     }
     pthread_join(printThread, NULL);
}                

This code prints ***got here***** as I expect. However, if I change printThreadFunction() to be:

void* printThreadFunction()
{
        puts("***got here*****");
        while(1); // change here       
}   

So the code enters an infinite loop, but no printing is being done to the screen- why is that?

John
  • 861
  • 1
  • 7
  • 19
  • Possible duplicate of [Is stdout line buffered, unbuffered or indeterminate by default?](https://stackoverflow.com/questions/3723795/is-stdout-line-buffered-unbuffered-or-indeterminate-by-default) – Andrew Henle Aug 02 '17 at 12:55
  • 2
    @Marian As you say `stdout` is line-buffered by default. But [`puts`](http://en.cppreference.com/w/c/io/puts) *do* write a trailing newline, so explicit flushing should not be needed. – Some programmer dude Aug 02 '17 at 12:55
  • To the OP: You do not redirect the output of the program to a file (using something similar to `./a.out > some_file` when running)? – Some programmer dude Aug 02 '17 at 12:59
  • try `fflush(stdout);` after puts. – Serge Aug 02 '17 at 13:04
  • Possibly related: [Cygwin terminal buffers STDOUT](https://stackoverflow.com/q/37706946/2402272) – John Bollinger Aug 02 '17 at 13:06

0 Answers0