0

Sometimes c++ plays me big time. I really can't think of why this does/doesn't work and I'd be happy if any of you knew.

I call this function once every second on a thread.

This code WORKS (prints what is on the list being iterated):

void DeltaList::print()
{
  pthread_mutex_lock (&mutex);
  printf("\n");
  list<Delta*>::iterator it;
  for(it=deltas.begin(); it!=deltas.end(); it++)
  {
    printf("%d   ", (int) (*it)->timeleft);
  }

  pthread_mutex_unlock (&mutex);
}

This one DOESN'T (nothing is printed):

void DeltaList::print()
{
  pthread_mutex_lock (&mutex);
  //printf("\n");
  list<Delta*>::iterator it;
  for(it=deltas.begin(); it!=deltas.end(); it++)
  {
    printf("%d   ", (int) (*it)->timeleft);
  }

  pthread_mutex_unlock (&mutex);
}

So... ?

hfingler
  • 1,931
  • 4
  • 29
  • 36
  • WORKS = really print what is on the list. DOESN'T WORK = does nothing, nothing is printed. the printf is the only part of the code i change. This was so logical to me that i forgot to put on the question, edited it now. – hfingler Nov 14 '11 at 21:32
  • Does it block forever? i.e. does it ever leave the function? – Oliver Charlesworth Nov 14 '11 at 21:33
  • 1
    The printf("\n") forces the output buffer to be flushed. What is probably happening is that it is printing to internal buffer and you are just not printing enough to force it to flush. Wait a while to fill the buffer or force it to flush. – Martin York Nov 14 '11 at 21:33
  • Loki is correct. The output is buffered, and until you print enough for the buffer to fill and flush, you won't see it without explicitly causing a flush with `\n` or a call to `fflush()`. – Brian Roach Nov 14 '11 at 21:35

3 Answers3

3

This has nothing to do with threading or C++. The OS is buffering your output, and the \n implicitly flushes the buffer when stdout is a console. Call fflush(stdout) after the loop if you want every call to show its output immediately.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • I'd never think of this.. just a quick question (i don't wanna try it now): what happens if i print too many characters (say the memory size) to stdout and no '\n'? – hfingler Nov 14 '11 at 21:42
  • The buffer also gets flushed when it fills up. – bames53 Nov 14 '11 at 21:49
  • `stdout` is also flushed on a call to `exit()` or a return from `main`, so the data should have been printed eventually. – Robᵩ Nov 15 '11 at 01:19
0

I guess that's because STDOUT is buffered, and printing \n flushes the buffer - see SO on printf \n behaviour

Community
  • 1
  • 1
Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
0

Switch over to using std::cerr as an experiment and I would bet you that it prints fine every time because standard error is not buffered while cout/printf (standard out) is buffered. Placing a std::endl at the end will buffer flush and put a new line out for cout which should work as well. You could also simply work on standard out and call a flush function on the stream to ensure its printed to console which would be similar.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255